首页 > web前端 > js教程 > 正文

jQuery实现多层验证后的表单验证

php中世界最好的语言
发布: 2018-03-15 17:32:04
原创
1353 人浏览过

这次给大家带来jQuery实现多层验证后的表单验证,jQuery实现多层验证后表单验证的注意事项有哪些,下面就是实战案例,一起来看一下。

本文实例为大家分享了jQuery实现表单验证的具体代码,供大家参考,具体内容如下

<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Reg</title>
  <style>
   .state1{
    color:#aaa;
   }
   .state2{
    color:#000;
   }
   .state3{
    color:red;
   }
   .state4{
    color:green;
   }
  </style>
  <script src="jquery.js"></script>
  <script>
   $(function(){
    var ok1=false;
    var ok2=false;
    var ok3=false;
    var ok4=false;
    // 验证用户名
    $('input[name="username"]').focus(function(){
     $(this).next().text('用户名应该为3-20位之间').removeClass('state1').addClass('state2');
    }).blur(function(){
     if($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=&#39;&#39;){
      $(this).next().text(&#39;输入成功&#39;).removeClass(&#39;state1&#39;).addClass(&#39;state4&#39;);
      ok1=true;
     }else{
      $(this).next().text(&#39;用户名应该为3-20位之间&#39;).removeClass(&#39;state1&#39;).addClass(&#39;state3&#39;);
     }    
    }); 
    //验证密码
    $(&#39;input[name="password"]&#39;).focus(function(){
     $(this).next().text(&#39;密码应该为6-20位之间&#39;).removeClass(&#39;state1&#39;).addClass(&#39;state2&#39;);
    }).blur(function(){
     if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=&#39;&#39;){
      $(this).next().text(&#39;输入成功&#39;).removeClass(&#39;state1&#39;).addClass(&#39;state4&#39;);
      ok2=true;
     }else{
      $(this).next().text(&#39;密码应该为6-20位之间&#39;).removeClass(&#39;state1&#39;).addClass(&#39;state3&#39;);
     }    
    });
    //验证确认密码
     $(&#39;input[name="repass"]&#39;).focus(function(){
     $(this).next().text(&#39;输入的确认密码要和上面的密码一致,规则也要相同&#39;).removeClass(&#39;state1&#39;).addClass(&#39;state2&#39;);
    }).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('输入的确认密码要和上面的密码一致,规则也要相同').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;
     }      
    });
    //提交按钮,所有验证通过方可提交
 
    $('.submit').click(function(){
     if(ok1 && ok2 && ok3 && ok4){
      $('form').submit();
     }else{
      return false;
     }
    });   
   });
  </script>
 </head>
<body> 
<form action=&#39;do.php&#39; method=&#39;post&#39; >
 用 户 名:<input type="text" name="username">
    <span class=&#39;state1&#39;>请输入用户名</span><br/><br/>
 密  码:<input type="password" name="password">
    <span class=&#39;state1&#39;>请输入密码</span><br/><br/>
 确认密码:<input type="password" name="repass">
    <span class=&#39;state1&#39;>请输入确认密码</span><br/><br/>
 邮  箱:<input type="text" name="email">
    <span class=&#39;state1&#39;>请输入邮箱</span><br/><br/> 
 <a href="javascript:;" rel="external nofollow" ><img class=&#39;submit&#39; type=&#39;image&#39; src=&#39;./images/reg.gif&#39; /></a>
</form>
</body>
</html>
登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

怎样用jQuery验证表单密码的一致性

Ajax怎么实现下拉框无刷新联动

无new构建详解

jQuery实现下拉菜单导航

以上是jQuery实现多层验证后的表单验证的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!