日期:2014-05-16 浏览次数:20760 次
remote: { url: "ajax/validateUserName.action", //后台处理程序 type: "post", //数据发送方式 dataType: "json", //接受数据格式 data: { //要传递的数据,默认已传递应用此规则的表单项 email: function() { return $("#email").val(); } }
注意:remote是远程验证:比如注册验证用户名是否已被注册,返回值只能是true(验证成功)或false(验证失败)。
除了内置的验证规则,validation还允许自定义验证规则。这是通过validation的addMethod方法实现的,语法为:jQuery.validator.addMethod("name",function,message)。其中name为验证规则的名称,function定义验证的规则,message是验证失败时的提示信息。
?
jqueryvalide例子:
?
$.validator.setDefaults({
?submitHandler: function() { alert("submitted!"); }
});
$().ready(function() {
?// validate the comment form when it is submitted
?$("#commentForm").validate();
?
?// validate signup form on keyup and submit
?$("#signupForm").validate({
??rules: {
???firstname: "required",
???lastname: "required",
???username: {
????required: true,
????minlength: 2
???},
???password: {
????required: true,
????minlength: 5
???},
???confirm_password: {
????required: true,
????minlength: 5,
????equalTo: "#password"
???},
???email: {
????required: true,
????email: true
???},
???topic: {
????required: "#newsletter:checked",
????minlength: 2
???},
???agree: "required"
??},
??messages: {
???firstname: "Please enter your firstname",
???lastname: "Please enter your lastname",
???username: {
????required: "Please enter a username",
????minlength: "Your username must consist of at least 2 characters"
???},
???password: {
????required: "Please provide a password",
????minlength: "Your password must be at least 5 characters long"
???},
???confirm_password: {
????required: "Please provide a password",
????minlength: "Your password must be at least 5 characters long",
????equalTo: "Please enter the same password as above"
???},
???email: "Please enter a valid email address",
???agree: "Please accept our policy"
??}
?});
?
?// propose username by combining first- and lastname
?$("#username").focus(function() {
??var firstname = $("#firstname").val();
??var lastname = $("#lastname").val();
??if(firstname && lastname && !this.value) {
???this.value = firstname + "." + lastname;
??}
?});
?
?//code to hide topic selection, disable for demo
?