jquery-validation-1.9.0

jQuery验证插件:
简单高效的客户端验证框架,提供很多属性来实现个性化效果。,显示尽可能少的错误提示信息来提示用户使得画面达到用户友好的效果。
[color=red]内置很多常用的验证方法[/color],如Email,Url等,同时也提供接口来[color=red]实现自己的验证方法,也可以调用服务器端方法来验证[/color]。
[color=red]支持国际化[/color],支持36种语言的错误提示,默认使用英语版的错误提示消息。
form提交的时候[color=red]默认焦点[/color]设置在第一个验证不通过的控件上(也可以倒着来),如果有2个控件验证都通不过,并且提交前焦点在后一个控件上,那么会自动把焦点设置在后一个控件上方便用户修改。
[color=red]值修改后自动验证以及多错误同时显示[/color]:在提交时,默认会把所有验证通不过的控件都标记出来,同时在修正的时候,如果修正后的值会通过验证,那么错误标记会自动消失。
[color=red]懒验证:[/color]在Form第一次提交之前,使用tab键切换控件的时候不会触发验证操作。[color=red]但是修改值的时候会自动调用验证方法进行验证。如果验证通过,相应的错误提示信息会自动消失。[/color]
$.validator.addMethod( name, method, [message] ) 可以实现[color=red]条件验证[/color]的效果,即根据条件动态增加、删除验证规则 使用例子见下面

插件中很重要的2个概念:
method:验证方法,实现真正的验证逻辑
rule:验证规则,控件关联的验证方法名,表示控件需要进行的验证操作
默认使用[color=red]class="required email"[/color]的方式来设置控件对应的验证规则

// 也可以在调用验证方法的时候,设置验证规则和关联控件,这时候冒号前面默认是控件名,对应控件的name属性!!! 对应jquery选择器?
$("#myform").validate({
rules: {
txtUrl: {url: true},
txtEmail: {email : true},
// no quoting necessary
name: "required",
// quoting necessary! 名字中有特殊字符的时候,需要把选择器用双引号包起来
"user[email]": "email",
// dots need quoting, too!
"user.address.street": "required"
}
});

默认内置的方法:

required( ) Returns: Boolean
Makes the element always required.
required( dependency-expression ) Returns: Boolean
Makes the element required, depending on the result of the given expression.
required( dependency-callback ) Returns: Boolean
Makes the element required, depending on the result of the given callback.
remote( options ) Returns: Boolean
Requests a resource to check the element for validity.
minlength( length ) Returns: Boolean
Makes the element require a given minimum length.
maxlength( length ) Returns: Boolean
Makes the element require a given maxmimum length.
rangelength( range ) Returns: Boolean
Makes the element require a given value range.
min( value ) Returns: Boolean
Makes the element require a given minimum.
max( value ) Returns: Boolean
Makes the element require a given maximum.
range( range ) Returns: Boolean
Makes the element require a given value range.
email( ) Returns: Boolean
Makes the element require a valid email
url( ) Returns: Boolean
Makes the element require a valid url
date( ) Returns: Boolean
Makes the element require a date.
dateISO( ) Returns: Boolean
Makes the element require a ISO date.
number( ) Returns: Boolean
Makes the element require a decimal number.
digits( ) Returns: Boolean
Makes the element require digits only.
creditcard( ) Returns: Boolean
Makes the element require a creditcard number.
accept( extension ) Returns: Boolean
Makes the element require a certain file extension.
equalTo( other ) Returns: Boolean
Requires the element to be the same as another one

更加多的方法是以插件的形式包含在additional-methods.js中提供的

分组验证,多个控件一起验证(对应一个验证方法,一个提示消息):[url=http://docs.jquery.com/Plugins/Validation/multiplefields]Plugins/Validation/multiplefields[/url]
重构验证(多个控件用相同的验证规则和提示消息):

// alias required to cRequired with new message
$.validator.addMethod("cRequired", $.validator.methods.required,
"Customer name required");
// alias minlength, too
$.validator.addMethod("cMinlength", $.validator.methods.minlength,
// leverage parameter replacement for minlength, {0} gets replaced with 2
$.format("Customer name must have at least {0} characters"));
// combine them both, including the parameter for minlength
$.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });


// 多个客户名输入框,有相同的class:customer
<input name="customer1" class="customer" />
<input name="customer2" class="customer" />
<input name="customer3" class="customer" />

错误提示消息的优先顺序:A custom message (passed by plugin options), the element's title, the default message.
[color=red]插件调试:[/color]把debug 选项值设置为true,不管验证是否通过,它都会阻止form的提交,并且输出一些有用的信息到window.console(可以通过firebug或者firebug lite看到)
当有多个form在画面时,插件默认每次只验证一个form,可以通过 $.validator.setDefaults({...}) 来修改默认行为,使得一次验证多个form
Ajax验证:

$("#myform").validate({
rules: {
username: {
required: true,
minLength: 2,
remote: "users.php"
}
},
messages: {
username: {
required: "Enter your username",
minLength: "At least 2 characters are necessary",
remote: String.format("The name {0} is already in use")
}
}
});

修改默认的提示信息:

$.extend($.validator.messages, {
required: "Eingabe nötig",
email: "Bitte eine gültige E-Mail-Adresse eingeben",
...
});


协议:MIT/GPL
官网:[url=http://bassistance.de/jquery-plugins/jquery-plugin-validation/]jQuery plugin: Validation[/url]
参考文档:[url=http://docs.jquery.com/Plugins/Validation]Plugins/Validation[/url]
演示Demo:[url=http://jquery.bassistance.de/validate/demo/]jQuery Validation Plugin Demo[/url]
FAQ:[url=http://docs.jquery.com/Plugins/Validation/Reference#Fields_with_complex_names_.28brackets.2C_dots.29]Plugins/Validation/Reference[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值