jQuery验证所有输入合法后才干提交
大学三年里所有在专注后台编码。学会不知多少种,servlet。ssh,springMVC,web.py......
最后每次碰到前端自己要写点东西就满目愁抑,
干脆自己好好理解一段前端代码,
特地拿出參考别人的一个可用的jQuery验证表单输入。
总体来说还是很easy。了解focus、blur的使用方法就足够,以后自己要填加一些更麻烦的前端jQuery组件运用。
下面代码參考搜索:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Reg</title>
<script src="jquery-2.1.0.min.js"></script>
<style>
.state1{
color:#aaa;
}
.state2{
color:#000;
}
.state3{
color:red;
}
.state4{
color:green;
}
</style>
<script>
$(function(){
var ok1=false;
var ok2=false;
var ok3=false;
var ok4=false;
// 验证username
$('input[name="username"]').focus(function(){ //focus()是获得焦点时对应的处理函数
$(this).next().text('username应该为3-20位之间').removeClass('state1').addClass('state2'); //$(this).next()当前元素同级的下个元素
}).blur(function(){ //元素失去焦点前触发blur函数
if($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=''){
$(this).next().text('输入成功').removeClass('state1').addClass('state4');
ok1=true;
}else{
$(this).next().text('username应该为3-20位之间').removeClass('state1').addClass('state3');
}
});
//验证password
$('input[name="password"]').focus(function(){
$(this).next().text('password应该为6-20位之间').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=''){
$(this).next().text('输入成功').removeClass('state1').addClass('state4');
ok2=true;
}else{
$(this).next().text('password应该为6-20位之间').removeClass('state1').addClass('state3');
}
});
//验证确认password
$('input[name="repass"]').focus(function(){
$(this).next().text('输入的确认password要和上面的password一致,规则也要同样').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!='' && $(this).val() == $('input[name="password"]').val()){
$(this).next().text('输入成功').removeClass('state1').addClass('state4');
ok3=true;
}else{
$(this).next().text('输入的确认password要和上面的password一致,规则也要同样').removeClass('state1').addClass('state3');
}
});
//验证邮箱
$('input[name="email"]').focus(function(){
$(this).next().text('请输入正确的EMAIL格式').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)==-1){
$(this).next().text('请输入正确的EMAIL格式').removeClass('state1').addClass('state3');
}else{
$(this).next().text('输入成功').removeClass('state1').addClass('state4');
ok4=true;
}
});
//提交button,全部验证通过方可提交
$('.submit').click(function(){
if(ok1 && ok2 && ok3 && ok4){
$('form').submit();
}else{
return false;
}
});
});
</script>
</head>
<body>
<form action='do.php' method='post' >
用 户 名:<input type="text" name="username">
<span class='state1'>请输入username</span><br/><br/>
密 码:<input type="password" name="password">
<span class='state1'>请输入password</span><br/><br/>
确认password:<input type="password" name="repass">
<span class='state1'>请输入确认password</span><br/><br/>
邮 箱:<input type="text" name="email">
<span class='state1'>请输入邮箱</span><br/><br/>
<a href="javascript:;"><img class='submit' />提交</a>
</form>
</body>
浙公网安备 33010602011771号