一、validate

  1.官方网站:http://jqueryvalidation.org/

  2.文档说明:http://jqueryvalidation.org/documentation/

  3.js文件下载:官方网站首页,提供一个最新版本的链接

二、验证表单的一般步骤

  1.准备好从JQuery官方网站下载JQuery.js文件和validate官网下载的jquery.validte.js文件

    注意事项:validate并不支持所有版本的JQuery,支持的版本目前有:JQuery-1.7.2,1.8.3,1.9.1,1.11.1,该信息可以从官网首页Required字段查找到。

  2.在网页代码中引入两个文件(不要使用自带的JQuery.js文件,该文件在之前的版本中使用比较方便,但是最新版本的JQuery.js文件里面没有任何实质性的内容,最后还是引入了外部的js文件,大多会使用JQuery-1.11.1.js文件,所以最好直接使用该js文件)。

  3.调用验证的方法

$("#empForm").validate(
{
//自定义规则
rules:{ },
//自定义提示信息
messages:{ }
}
);

三、validate的系统规则详情(可参考官网doc)

  1.英文原版

  2.中文版

  (1)required:true,必须字段

  (2)remote:"check.jsp",使用ajax方法调用check.jsp验证输入

  (3)email:true,必须输入正确格式的电子邮件

  (4)url:true,必须输入正确格式的网址

  (5)date:true,必须输入正确格式的日期

  (6)dateISO:必须输入正确格式的日期(ISO),例如:2009-1-1,1009/1/1,只验证格式,不验证有效性

  (7)number:true,必须输入合法的数字

  (8)digits:true,必须输入整数

  (9)creditcard:true,必须输入合法的信用卡号

  (10)equalTo:"#filed",输入的值必须和filed的值相同

  (11)accept:输入拥有合法后缀名的字符串

  (12)maxlength:5,输入长度最多是5的字符串(汉字算一个字符)

  (13)minlength:10,输入长度最多是最小是10的字符串(汉字算一个字符)

  (14)rangelength:[5,10],输入长度必须在5到10之间的字符串,汉字算一个字符。

  (15)range:[5,10],输入的值必须在5到10之间

  (16)max:5,输入的值最大不能大到5

  (17)min:5,输入的值最小不能小于5

  3.使用样例

rules:{
realname:{
required:true
},
username:{
required:true,
rangelength:[5,8]
},
psw:{
required:true,
rangelength:[6,12]
},
psw2:{
required:true,
rangelength:[6,12],
equalTo:"#psw"
},
gender:{
required:true
},
age:{
required:true,
range:[26,50]
},
edu:{
required:true
},
birthday:{
required:true,
dateISO:true
},
checkbox1:{
required:true
},
email:{
required:true,
email:true
},
cart:{
required:true,
checkIdLength:true,
checkId:true
}
},

四、提示信息messages的写法

  写法格式和rules相同,但是在每个方法之后需要写上字符串,用于提示信息用。

  例:

messages:{
realname:{
required:"真实姓名不能为空!"
},
username:{
required:"登录名不能为空!",
rangelength:"登录名长度应当在5-8之间"
},
psw:{
required:"密码不能为空!",
rangelength:"密码长度为6-12!"
},
psw2:{
required:"请再次输入密码!",
rangelength:"密码长度为6-12!",
equalTo:"两次输入的密码不一致!"
},
gender:{
required:"性别必须进行选择!"
},
age:{
required:"年龄不能为空!",
range:"年龄范围应在26-50之间!"
},
edu:{
required:"请选择学历信息!",
},
birthday:{
required:"请输入日期信息!",
dateISO:"请输入正确格式的日期!"
},
checkbox1:{
required:"请至少选择一个兴趣爱好!"
},
email:{
required:"电子邮箱不能为空!",
email:"请输入正确的电子邮箱!"
},
cart:{
required:"身份证号码不能为空!",
checkIdLength:"身份证号码长度应为15或者18",
checkId:"身份证号码不合法!"
}
}
}

五、自定义规则

  1.流程

    (1)在定义校验规则之前先定义一个方法用于执行校验规则的逻辑

    (2)在rules中指定某个域使用该校验

    (3)在messages中指定这个域使用此校验规则没有通过的提示信息

  2.自定义校验规则的格式

$.validator.addMethod("规则名称,如required",function(value,element,params)
{
//value是元素的值,如text格式的input元素value值
//element是元素本身,如Input
//params是指在rules中设置的参数
},"错误提示信息");

  3.示例:

    (1)定义方法

$.validator.addMethod("checkIdLength",function(value,element,params){
if(value.length!=15&&value.length!=18)
return false;
return true;
},"长度错误!");

    (2)rules中指定某个域使用该校验

          cart:{
required:true,
checkIdLength:true
}

    (3)在messages中定义错误提示信息

           cart:{
required:"身份证号码不能为空!",
checkIdLength:"身份证号码长度应为15或者18"
}

六、综合练习

 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery validation plug-in - main demo</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" />
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript">
/*********************************************************************************************************/ </script>
</head>
<body>
<p>员工信息录入</p>
<form name="empForm" id="empForm" method="post" action="test.html">
<table border=1>
<tr>
<td><label for="realname">真实姓名(不能为空 ,没有其他要求)</label></td>
<td><input type="text" id="realname" name="realname" />
</td>
</tr>
<tr>
<td>
<label for="username">登录名(登录名不能为空,长度应该在5-8之间,可以包含中文字符(一个汉字算一个字符)):</label>
</td>
<td><input type="text" id="username" name="username" /></td>
</tr>
<tr>
<td>
<label for="psw">密码(不能为空,长度6-12字符或数字,不能包含中文字符):</label>
</td>
<td><input type="password" id="psw" name="psw" style="width:120px" /></td>
</tr>
<tr>
<td>重复密码密码(不能为空,长度6-12字符或数字,不能包含中文字符):</td>
<td><input type="password" id="psw2" name="psw2" style="width:120px" /></td>
</tr>
<!-- -->
<tr>
<td>性别(必选其一)</td>
<td>
<input type="radio" id="gender_male" value="m" name="gender"/>男
<input type="radio" id="gender_female" value="f" name="gender"/>女
<label style="display: none" for="gender" class="error">请选择性别</label>
</td>
</tr>
<tr>
<td>年龄(必填26-50):</td>
<td><input type="text" id="age" name="age" /></td>
</tr> <tr>
<td>你的学历:</td>
<td> <select name="edu" id="edu">
<option value="">--请选择你的学历--</option>
<option value="a">专科</option>
<option value="b">本科</option>
<option value="c">研究生</option>
<option value="e">硕士</option>
<option value="d">博士</option>
</select>
</td>
</tr> <tr>
<td>出生日期(1982/09/21):</td>
<td><input type="text" id="birthday" name="birthday" style="width:120px" value="" /></td>
</tr> <tr>
<td>兴趣爱好:</td>
<td colspan="2">
<input type="checkbox" name="checkbox1" id="qq1"/>乒乓球
<input type="checkbox" name="checkbox1" id="qq2" value="1" />羽毛球
<input type="checkbox" name="checkbox1" id="qq3" value="2" />上网
<input type="checkbox" name="checkbox1" id="qq4" value="3" />旅游
<input type="checkbox" name="checkbox1" id="qq5" value="4" />购物
<label style="display: none" for="checkbox1" class="error">您的兴趣爱好,至少选择一个</label>
</td>
</tr>
<tr>
<td align="left">电子邮箱:</td>
<td><input type="text" id="email" style="width:120px" name="email" /></td>
</tr>
<tr>
<td align="left">身份证(15-18):</td>
<td><input type="text" id="cart" style="width:200px" name="cart" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="firstname" id="firstname" value="保存"></td>
</tr>
</table> </form>
<script type="text/javascript">
$.validator.addMethod("checkIdLength",function(value,element,params){
if(value.length!=15&&value.length!=18)
return false;
return true;
},"长度错误!");
$.validator.addMethod("checkId",function(value,element,params){
if(value.length==15){
//正则表达式不能带上引号
var pattern=/^\d{15}$/;
//alert("长度为15!");
var result=pattern.test(value);
return result;
}
if(value.length==18){
var pattern=/^\d{18}|\d{17}[X|x]$/;
var result=pattern.test(value);
return result;
}
return false;
});
</script>
<script type="text/javascript">
/* $(document).ready(function(){
alert("开始加载页面!");
}); */
$("#empForm").validate({
//定义规则
rules:{
realname:{
required:true
},
username:{
required:true,
rangelength:[5,8]
},
psw:{
required:true,
rangelength:[6,12]
},
psw2:{
required:true,
rangelength:[6,12],
equalTo:"#psw"
},
gender:{
required:true
},
age:{
required:true,
range:[26,50]
},
edu:{
required:true
},
birthday:{
required:true,
dateISO:true
},
checkbox1:{
required:true
},
email:{
required:true,
email:true
},
cart:{
required:true,
checkIdLength:true,
checkId:true
}
},
//定义犯错的时候使用的提示信息
messages:{
realname:{
required:"真实姓名不能为空!"
},
username:{
required:"登录名不能为空!",
rangelength:"登录名长度应当在5-8之间"
},
psw:{
required:"密码不能为空!",
rangelength:"密码长度为6-12!"
},
psw2:{
required:"请再次输入密码!",
rangelength:"密码长度为6-12!",
equalTo:"两次输入的密码不一致!"
},
gender:{
required:"性别必须进行选择!"
},
age:{
required:"年龄不能为空!",
range:"年龄范围应在26-50之间!"
},
edu:{
required:"请选择学历信息!",
},
birthday:{
required:"请输入日期信息!",
dateISO:"请输入正确格式的日期!"
},
checkbox1:{
required:"请至少选择一个兴趣爱好!"
},
email:{
required:"电子邮箱不能为空!",
email:"请输入正确的电子邮箱!"
},
cart:{
required:"身份证号码不能为空!",
checkIdLength:"身份证号码长度应为15或者18",
checkId:"身份证号码不合法!"
}
}
});
</script>
</body>
</html>

validate_form.html

运行结果:

七、总结一些注意事项

  1.实际上对域的监听是通过<label></label>标签实现的,所以尽量要在被监听的元素旁边声明Label标签,如<label for="username">用户名</label>,其中for的名字要和被监听域的name属性相同,如<input type="text" name="username">,事实上如果没有改标签的话validate插件会自动检测并通过dom添加一个label标签。

  2.对于下拉列表框,如果让某一个option元素的值变成"",那么该元素就不会被监听,这样就算是单击了该项,也不会被认为执行了选择的动作,这种情况适用于提示的option,如<option value="">请选择一个省份</option>。

  3.规则添加的顺序决定了该规则的执行顺序,所以要谨慎选择添加规则选项的顺序。

  4.不要使用""将正则表达式引起来,否则后果很严重。使用形式:/^内容$/

  5.单选框、复选框必须加上label标签,下拉菜单则可以不用。

  单选框:

<input  type="radio" id="gender_male" value="m" name="gender"/>男
<input type="radio" id="gender_female" value="f" name="gender"/>女
<label style="display: none" for="gender" class="error">请选择性别</label>

  复选框:

 <input type="checkbox" name="checkbox1" id="qq1"/>乒乓球
<input type="checkbox" name="checkbox1" id="qq2" value="1" />羽毛球
<input type="checkbox" name="checkbox1" id="qq3" value="2" />上网
<input type="checkbox" name="checkbox1" id="qq4" value="3" />旅游
<input type="checkbox" name="checkbox1" id="qq5" value="4" />购物
<label style="display: none" for="checkbox1" class="error">您的兴趣爱好,至少选择一个</label>

【Java EE 学习 33 下】【validate表单验证插件】的更多相关文章

  1. jQuery学习之:Validation表单验证插件

    http://polaris.blog.51cto.com/1146394/258781/ 最近由于公司决定使用AJAX + Struts2来重构项目,让我仔细研究一下这两个,然后集中给同事讲讲,让每 ...

  2. jQuery Validate 表单验证插件----Validate简介,官方文档,官方下载地址

     一. jQuery Validate 插件的介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆 ...

  3. jquery validate表单验证插件-推荐

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  4. jquery validate表单验证插件

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  5. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

  6. jQuery Validate 表单验证插件----自定义一个验证方法

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  7. jQuery Validate 表单验证插件----自定义校验结果样式

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  8. jQuery Validate 表单验证插件----通过name属性来关联字段来验证,改变默认的提示信息,将校验规则写到 js 代码中

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二. 添加一个另外一个插件jquery.validate.messages_cn.js. ...

  9. jQuery Validate 表单验证插件----利用jquery.metadata.js将校验规则直接写在class属性里面并定义错误信息的提示

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二. 添加一个另外一个插件jquery.metadata.js 并把校验规则写在控件里面 ...

  10. jQuery Validate 表单验证插件----在class属性中添加校验规则进行简单的校验

    一.下载插件包. 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.jQuery表单验证插件----添加class属性形式的校验 <!DOCTY ...

随机推荐

  1. 使用VS Code 从零开始开发并调试.NET Core 应用程序

    最新文章:http://www.cnblogs.com/linezero/p/VSCodeNETCore.html 使用VS Code 从零开始开发并调试.NET Core 应用程序,C#调试. 上一 ...

  2. JSF primefaces session view expired 会话失效后页面跳转

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  3. maven的eclise配置

    http://blog.csdn.net/guanning0109/article/details/26069277

  4. Kafka实战-Flume到Kafka

    1.概述 前面给大家介绍了整个Kafka项目的开发流程,今天给大家分享Kafka如何获取数据源,即Kafka生产数据.下面是今天要分享的目录: 数据来源 Flume到Kafka 数据源加载 预览 下面 ...

  5. Python Day6

    面向对象 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强...&qu ...

  6. Java开发的基础条件:

    ------------Java开发的基础条件:Java相关的基础+对编程的自己的理解+调试代码+自己的坚持 一定要谦逊,不人云亦云,不去妄言某一门语言或技术好或坏!不是哪门技术有问题,而是(不会用才 ...

  7. Unable to load configuration. - Class: java.net.AbstractPlainSocketImpl

    [Bug笔记]Unable to load configuration. - Class: java.net.AbstractPlainSocketImpl 标签: bugjartomcat服务器互联 ...

  8. TCP中的RST复位信号

    TCP中的RST复位信号 在TCP协议中RST表示复位,用来关闭异常的连接,在TCP的设计中它是不可或缺的. 发送RST包关闭连接时,不必等缓冲区的包都发出去,直接就丢弃缓存区的包发送RST包.而接收 ...

  9. MySql 杂记

    1:声明一个int变量时,设置它默认为0,而不是空或null. int 型,取值范围-2,147,483,648 到 2,147,483,647 ,默认值是 0 int是值类型,读内存区间中指定长度单 ...

  10. Angular2.0快速开始

    参考资料: Angular2.0快速开始 AngularJS2 教程