jQuery实现所有验证通过方可提交的表单验证


Posted in jQuery onNovember 21, 2017

本文实例为大家分享了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()!=''){
      $(this).next().text('输入成功').removeClass('state1').addClass('state4');
      ok1=true;
     }else{
      $(this).next().text('用户名应该为3-20位之间').removeClass('state1').addClass('state3');
     }
      
    });
 
    //验证密码
    $('input[name="password"]').focus(function(){
     $(this).next().text('密码应该为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('密码应该为6-20位之间').removeClass('state1').addClass('state3');
     }
      
    });
 
    //验证确认密码
     $('input[name="repass"]').focus(function(){
     $(this).next().text('输入的确认密码要和上面的密码一致,规则也要相同').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('输入的确认密码要和上面的密码一致,规则也要相同').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='do.php' method='post' >
 用 户 名:<input type="text" name="username">
    <span class='state1'>请输入用户名</span><br/><br/>
 密码:<input type="password" name="password">
    <span class='state1'>请输入密码</span><br/><br/>
 确认密码:<input type="password" name="repass">
    <span class='state1'>请输入确认密码</span><br/><br/>
 邮
箱:<input type="text" name="email">
    <span class='state1'>请输入邮箱</span><br/><br/> 
 <a href="javascript:;" rel="external nofollow" ><img class='submit' type='image' src='./images/reg.gif' /></a>
</form>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jquery单击文字或图片内容放大并居中显示
Jun 23 jQuery
解决jquery appaend元素中id绑定事件失效的问题
Sep 12 jQuery
jQuery使用zTree插件实现可拖拽的树示例
Sep 23 jQuery
jQuery实现html双向绑定功能示例
Oct 09 jQuery
jQuery实现checkbox即点即改批量删除及中间遇到的坑
Nov 11 jQuery
jQuery实现checkbox的简单操作
Nov 18 jQuery
jQuery简单判断值是否存在于数组中的方法示例
Apr 17 jQuery
jQuery中图片展示插件highslide.js的简单dom
Apr 22 jQuery
JQuery特殊效果和链式调用操作示例
May 13 jQuery
使用JQuery自动完成插件Auto Complete详解
Jun 18 jQuery
JQuery复选框全选效果如何实现
May 08 jQuery
jQuery实现全选按钮
Jan 01 jQuery
jquery-file-upload 文件上传带进度条效果
Nov 21 #jQuery
jquery ajaxfileupload异步上传插件
Nov 21 #jQuery
jquery中有哪些api jQuery主要API
Nov 20 #jQuery
jquery ztree实现右键收藏功能
Nov 20 #jQuery
jQuery实现checkbox的简单操作
Nov 18 #jQuery
基于jquery实现五星好评
Nov 18 #jQuery
jQuery实现滚动效果
Nov 17 #jQuery
You might like
PHP管理依赖(dependency)关系工具 Composer的自动加载(autoload)
2014/08/18 PHP
推荐几款用 Sublime Text 开发 Laravel 所用到的插件
2014/10/30 PHP
PHP使用PDO连接ACCESS数据库
2015/03/05 PHP
PHP使用内置函数生成图片的方法详解
2016/05/09 PHP
PHP 进度条函数的简单实例
2017/09/19 PHP
用js怎么把&amp;字符换成&quot;&amp;amp:&quot;
2006/10/19 Javascript
jquery聚焦文本框与扩展文本框聚焦方法
2012/10/12 Javascript
javascript date格式化示例
2013/09/25 Javascript
简单的js表单验证函数
2013/10/28 Javascript
js动态添加删除,后台取数据(示例代码)
2013/11/25 Javascript
js动态删除div元素基本思路及实现代码
2014/05/08 Javascript
Ubuntu中搭建Nodejs开发环境过程分享
2014/06/01 NodeJs
JavaScript中定义函数的三种方法
2015/03/12 Javascript
JS实现仿QQ面板的手风琴效果折叠菜单代码
2015/09/11 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
简单谈谈JS数组中的indexOf方法
2016/10/13 Javascript
ES6新特性四:变量的解构赋值实例
2017/04/21 Javascript
js实现鼠标拖拽div左右滑动
2020/01/15 Javascript
Vue简单实现原理详解
2020/05/07 Javascript
[01:11:46]DOTA2-DPC中国联赛 正赛 iG vs Magma BO3 第一场 2月23日
2021/03/11 DOTA
python实现事件驱动
2018/11/21 Python
浅谈python标准库--functools.partial
2019/03/13 Python
Python字符串处理的8招秘籍(小结)
2019/08/13 Python
python 正则表达式贪婪模式与非贪婪模式原理、用法实例分析
2019/10/14 Python
Django使用Celery加redis执行异步任务的实例内容
2020/02/20 Python
python 实现性别识别
2020/11/21 Python
python的dict判断key是否存在的方法
2020/12/09 Python
纯css3实现宠物小鸡实例代码
2018/10/08 HTML / CSS
法国女性内衣购物网站:Glamuse
2019/05/13 全球购物
机械电子工程专业推荐信范文
2013/11/20 职场文书
四群教育工作实施方案
2014/03/26 职场文书
消防宣传口号
2014/06/16 职场文书
2014年设备管理工作总结
2014/11/26 职场文书
鲁迅故居导游词
2015/02/05 职场文书
先进个人主要事迹怎么写
2015/11/04 职场文书
MySQL数据迁移相关总结
2021/04/29 MySQL