首页 > 其他 > 详细

onsubmit表单验证

时间:2014-10-21 11:41:51      阅读:221      评论:0      收藏:0      [点我收藏+]

<script type="text/javascript">
function check(){
    var username=document.getElementById(‘username‘);
    var password=document.getElementById(‘password‘);
    //防止用户输入空格也验证通过
    if (!(username.value.replace(/\s*/g,‘‘)&&password.value.replace(/\s*/g,‘‘))){
        username.focus();
        return false;
    }else{
        //document.forms[0].login.disabled=true;
        document.getElementById(‘login‘).disabled=true;
        document.getElementById(‘login‘).value=‘登录中‘;
        return true;
    }
}
</script>

<form action="test.php" method="get" id="test" onsubmit="return check()">
<label for="username">用户名 : </label><input id=‘username‘ name="username" type="text" />
<label for="password">密 码 : </label><input id="password" name="password" type="password"/>
<input type="submit" value="登陆" id="login" name="login" />
</form>

<!--<button type="submit">提交</button>
下面的默认不会触发onsubmit()事件
<input type=‘button‘ value=‘提交‘/>
<button onclick="_submit()">提交</button>-->

 

非行间事件的写法

var obj = document.getElementById(‘myform‘);
var check = function(){
    var username=document.getElementById(‘username‘);
    var password=document.getElementById(‘password‘);
    if (!(username.value.replace(/\s*/g,‘‘)&&password.value.replace(/\s*/g,‘‘))){
        return false;
    }else{
        return true;
    }
}
obj.onsubmit = function(){
return check();
}

// 这样写不能实现阻止表单提交
// obj.onsubmit = function(){
//     var username=document.getElementById(‘username‘);
//     var password=document.getElementById(‘password‘);
//     if (!(username.value.replace(/\s*/g,‘‘)&&password.value.replace(/\s*/g,‘‘))){
//         return false;
//     }else{
//         return true;
//     }
// }

onsubmit表单验证

原文:http://www.cnblogs.com/cdwp8/p/4039743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!