之前一篇文章介绍了jQuery Validate表单验证入门的基础知识,详细内容参见《jQuery Validate表单验证入门学习》,今天这篇文章深入学习jQuery Validate表单验证,以下就是文章的全部内容:
1、用其他方式替代默认的 SUBMIT
1 2 3 4 5 6 7 8 | $().ready( function () {
$( "#signupForm" ).validate({
submitHandler: function (form){
alert( "submitted" );
form.submit();
}
});
});
|
登录后复制
使用 ajax 方式
1 2 3 4 5 6 | $( ".selector" ).validate({
submitHandler: function (form)
{
$(form).ajaxSubmit();
}
})
|
登录后复制
可以设置 validate 的默认值,写法如下:
1 2 3 | $.validator.setDefaults({
submitHandler: function (form) { alert( "submitted!" );form.submit(); }
});
|
登录后复制
如果想提交表单, 需要使用 form.submit(),而不要使用 $(form).submit()。
2、debug,只验证不提交表单
如果这个参数为true,那么表单不会提交,只进行检查,调试时十分方便。
1 2 3 4 5 | $().ready( function () {
$( "#signupForm" ).validate({
debug:true
});
});
|
登录后复制
如果一个页面中有多个表单都想设置成为 debug,则使用:
1 2 3 | $.validator.setDefaults({
debug: true
})
|
登录后复制
3、ignore:忽略某些元素不验证
ignore: ".ignore"
4、更改错误信息显示的位置
errorPlacement:Callback
指明错误放置的位置,默认情况是:error.appendTo(element.parent());即把错误信息放在验证的元素后面。
1 2 3 | errorPlacement: function (error, element) {
error.appendTo(element.parent());
}
|
登录后复制
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <tr>
<td class = "label" ><label id= "lfirstname" for = "firstname" >First Name</label></td>
<td class = "field" ><input id= "firstname" name= "firstname" type= "text" value= "" maxlength= "100" /></td>
<td class = "status" ></td>
</tr>
<tr>
<td style= "padding-right: 5px;" >
<input id= "dateformat_eu" name= "dateformat" type= "radio" value= "0" />
<label id= "ldateformat_eu" for = "dateformat_eu" >14/02/07</label>
</td>
<td style= "padding-left: 5px;" >
<input id= "dateformat_am" name= "dateformat" type= "radio" value= "1" />
<label id= "ldateformat_am" for = "dateformat_am" >02/14/07</label>
</td>
<td></td>
</tr>
<tr>
<td class = "label" > </td>
<td class = "field" colspan= "2" >
<div id= "termswrap" >
<input id= "terms" type= "checkbox" name= "terms" />
<label id= "lterms" for = "terms" >I have read and accept the Terms of Use.</label>
</div>
</td>
</tr>
errorPlacement: function (error, element) {
if ( element.is( ":radio" ) )
error.appendTo( element.parent().next().next() );
else if ( element.is( ":checkbox" ) )
error.appendTo ( element.next() );
else
error.appendTo( element.parent().next() );
}
|
登录后复制
代码的作用是:一般情况下把错误信息显示在 中,如果是 radio 则显示在 中,如果是 checkbox 则显示在内容的后面。
参数 类型 描述 默认值
errorClass String 指定错误提示的 css 类名,可以自定义错误提示的样式。 "error"
errorElement String 用什么标签标记错误,默认是 label,可以改成 em。 "label"
errorContainer Selector 显示或者隐藏验证信息,可以自动实现有错误信息出现时把容器属性变为显示,无错误时隐藏,用处不大。
errorContainer: "#messageBox1, #messageBox2"
errorLabelContainer Selector 把错误信息统一放在一个容器里面。
wrapper String 用什么标签再把上边的 errorELement 包起来。
一般这三个属性同时使用,实现在一个容器内显示所有错误提示的功能,并且没有信息时自动隐藏。
errorContainer: "div.error",
errorLabelContainer: $("#signupForm div.error"),
wrapper: "li"
5、更改错误信息显示的样式
设置错误提示的样式,可以增加图标显示,在该系统中已经建立了一个 validation.css,专门用于维护校验文件的样式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | input.error { border: 1px solid red; }
label.error {
background:url( "./demo/images/unchecked.gif" ) no-repeat 0px 0px;
padding-left: 16px;
padding-bottom: 2px;
font-weight: bold;
color: #EA5200;
}
label.checked {
background:url( "./demo/images/checked.gif" ) no-repeat 0px 0px;
}
|
登录后复制
6、每个字段验证通过执行函数
success:String,Callback
要验证的元素通过验证后的动作,如果跟一个字符串,会当作一个 css 类,也可跟一个函数。
1 2 3 4 5 | success: function (label) {
label.html( " " ).addClass( "checked" );
}
|
登录后复制
添加 "valid" 到验证元素,在 CSS 中定义的样式 。
success: "valid"
7、验证的触发方式修改
下面的虽然是 boolean 型的,但建议除非要改为 false,否则别乱添加。
触发方式 类型 描述 默认值
onsubmit Boolean 提交时验证。设置为 false 就用其他方法去验证。 true
onfocusout Boolean 失去焦点时验证(不包括复选框/单选按钮)。 true
onkeyup Boolean 在 keyup 时验证。 true
onclick Boolean 在点击复选框和单选按钮时验证。 true
focusInvalid Boolean 提交表单后,未通过验证的表单(第一个或提交之前获得焦点的未通过验证的表单)会获得焦点。 true
focusCleanup Boolean 如果是 true 那么当未通过验证的元素获得焦点时,移除错误提示。避免和 focusInvalid 一起用。 false
1 2 3 4 5 6 7 8 9 10 11 12 13 | $().ready( function () {
var validator = $( "#signupForm" ).validate({
submitHandler: function (form){
alert( "submitted" );
form.submit();
}
});
$( "#reset" ).click( function () {
validator.resetForm();
});
});
|
登录后复制
8、异步验证
remote:URL
使用 ajax 方式进行验证,默认会提交当前验证的值到远程地址,如果需要提交其他的值,可以使用 data 选项。
1 2 3 4 5 6 7 8 9 10 11 | remote: "check-email.php"
remote: {
url: "check-email.php" ,
type: "post" ,
dataType: "json" ,
data: {
username: function () {
return $( "#username" ).val();
}
}
}
|
登录后复制
远程地址只能输出 "true" 或 "false",不能有其他输出。
9、添加自定义校验
addMethod:name, method, message
自定义验证方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | jQuery.validator.addMethod( "byteRangeLength" , function (value, element, param) {
var length = value.length;
for ( var i = 0; i < value.length; i++){
if (value.charCodeAt(i) > 127){
length++;
}
}
return this.optional(element) || ( length >= param[0] && length <= param[1] );
}, $.validator.format( "请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)" ));
jQuery.validator.addMethod( "isZipCode" , function (value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码" );
|
登录后复制
注意:要在 additional-methods.js 文件中添加或者在 jquery.validate.js 文件中添加。建议一般写在 additional-methods.js 文件中。
注意:在 messages_cn.js 文件中添加:isZipCode: "只能包括中文字、英文字母、数字和下划线"。调用前要添加对 additional-methods.js 文件的引用。
10、radio 和 checkbox、select 的验证
radio 的 required 表示必须选中一个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <input type= "radio" id= "gender_male" value= "m" name= "gender" class = "{required:true}" />
<input type= "radio" id= "gender_female" value= "f" name= "gender" />
checkbox 的 required 表示必须选中。
<input type= "checkbox" class = "checkbox" id= "agree" name= "agree" class = "{required:true}" />
checkbox 的 minlength 表示必须选中的最小个数,maxlength 表示最大的选中个数,rangelength:[2,3] 表示选中个数区间。
<input type= "checkbox" class = "checkbox" id= "spam_email" value= "email" name= "spam[]" class = "{required:true, minlength:2}" />
<input type= "checkbox" class = "checkbox" id= "spam_phone" value= "phone" name= "spam[]" />
<input type= "checkbox" class = "checkbox" id= "spam_mail" value= "mail" name= "spam[]" />
select 的 required 表示选中的 value 不能为空。
<select id= "jungle" name= "jungle" title= "Please select something!" class = "{required:true}" >
<option value= "" ></option>
<option value= "1" >Buga</option>
<option value= "2" >Baga</option>
<option value= "3" >Oi</option>
</select>
select 的 minlength 表示选中的最小个数(可多选的 select),maxlength 表示最大的选中个数,rangelength:[2,3] 表示选中个数区间。
<select id= "fruit" name= "fruit" title= "Please select at least two fruits" class = "{required:true, minlength:2}" multiple= "multiple" >
<option value= "b" >Banana</option>
<option value= "a" >Apple</option>
<option value= "p" >Peach</option>
<option value= "t" >Turtle</option>
</select>
|
登录后复制
附表:内置验证方式:

以上就是针对jQuery Validate表单验证的深入学习,希望对大家的学习有所帮助。