封Ja葬va

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

默认校验规则:

(1)   required:true 必输字段
(2)   remote:”check.php” 使用ajax方法调用check.php验证输入值
(3)   email:true 必须输入正确格式的电子邮件
(4)   url:true 必须输入正确格式的网址
(5)   date:true 必须输入正确格式的日期
(6)   dateISO:true 必须输入正确格式的日期(ISO),例如:2009-06-23,1998/01/22 只验证格式,不验证有效性
(7)   number:true 必须输入合法的数字(负数,小数)
(8)   digits:true 必须输入整数
(9)   creditcard: 必须输入合法的信用卡号
(10) equalTo:”#field” 输入值必须和#field相同
(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:10 输入值不能小于10

默认的提示

messages: {
       required: “This field is required.”,
       remote: “Please fix this field.”,
       email: “Please enter a valid email address.”,
       url: “Please enter a valid URL.”,
       date: “Please enter a valid date.”,
       dateISO: “Please enter a valid date (ISO).”,
       dateDE: “Bitte geben Sie ein g眉ltiges Datum ein.”,
       number: “Please enter a valid number.”,
       numberDE: “Bitte geben Sie eine Nummer ein.”,
       digits: “Please enter only digits”,
       creditcard: “Please enter a valid credit card number.”,
       equalTo: “Please enter the same value again.”,
       accept: “Please enter a value with a valid extension.”,
       maxlength: $.validator.format(“Please enter no more than {0} characters.”),
       minlength: $.validator.format(“Please enter at least {0} characters.”),
       rangelength: $.validator.format(“Please enter a value between {0} and {1} characters long.”),
       range: $.validator.format(“Please enter a value between {0} and {1}.”),
       max: $.validator.format(“Please enter a value less than or equal to {0}.”),
       min: $.validator.format(“Please enter a value greater than or equal to {0}.”)
}

修改默认提示

可在jquery.validate.js末尾加入:

jQuery.extend(jQuery.validator.messages, {
required: “必选字段”,
remote: “请修正该字段”,
email: “请输入正确格式的电子邮件”,
url: “请输入合法的网址”,
date: “请输入合法的日期”,
dateISO: “请输入合法的日期 (ISO).”,
number: “请输入合法的数字”,
digits: “只能输入整数”,
creditcard: “请输入合法的信用卡号”,
equalTo: “请再次输入相同的值”,
accept: “请输入拥有合法后缀名的字符串”,
maxlength: jQuery.validator.format(“请输入一个长度最多是 {0} 的字符串”),
minlength: jQuery.validator.format(“请输入一个长度最少是 {0} 的字符串”),
rangelength: jQuery.validator.format(“请输入一个长度介于 {0} 和 {1} 之间的字符串”),
range: jQuery.validator.format(“请输入一个介于 {0} 和 {1} 之间的值”),
max: jQuery.validator.format(“请输入一个最大为 {0} 的值”),
min: jQuery.validator.format(“请输入一个最小为 {0} 的值”)
});

推荐做法,将此文件放入messages_cn.js中,在页面中引入

<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../js/validate/jquery.validate.js"></script>
<script type="text/javascript" src="../js/validate/message_cn.js"></script>

表单验证使用方式

1.将验证规则写在控件中:

<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../js/validate/jquery.validate.js"></script>
<script type="text/javascript" src="../js/validate/message_cn.js"></script>
<script type="text/javascript">
$(function(){
$("#commentForm").validate();
});
</script>
<form id="commentForm" method="post" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" class="required" minlength="2" />
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" class="required" />
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url" />
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" class="required"></textarea>
</p>
<p class="btns">
<input type="submit" value="Submit"/>
<input type="submit" value="Submit"/>
</p>
</fieldset>
</form>

如果使用class=”{}”的方式,必须引入包:jquery.metadata.js

可以使用如下的方法,修改提示内容:
class=”{required:true,minlength:5,messages:{required:’请输入内容’}}”

在使用equalTo关键字时,后面的内容必须加上引号,如下代码:
class=”{required:true,minlength:5,equalTo:’#password’}”

2.将校验规则写在JS中:

<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../js/validate/jquery.validate.js"></script>
<script type="text/javascript" src="../js/validate/message_cn.js"></script>
<script type="text/javascript">
$(function(){
$("#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"
}
});
});
</script>
<form class="bg_form" id="signupForm" method="get" action="">
<fieldset>
<legend>Validating a complete form</legend>
<p>
<label for="firstname">Firstname</label>
<input id="firstname" name="firstname" />
</p>
<p>
<label for="lastname">Lastname</label>
<input id="lastname" name="lastname" />
</p>
<p>
<label for="username">Username</label>
<input id="username" name="username" />
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password" />
</p>
<p>
<label for="confirm_password">Confirm password</label>
<input id="confirm_password" name="confirm_password" type="password" />
</p>
<p>
<label for="email">Email</label>
<input id="email" name="email" type="text" />
</p>
<p>
<label for="agree">Please agree to our policy</label>
<input type="checkbox" class="checkbox" id="agree" name="agree" />
</p>
<p>
<label for="newsletter">I'd like to receive the newsletter</label>
<input type="checkbox" class="checkbox" id="newsletter" name="newsletter" />
</p>
<fieldset id="newsletter_topics">
<legend>Topics (select at least two) - note: would be hidden when newsletter isn't selected, but is visible here for the demo</legend>
<label for="topic_marketflash">
<input type="checkbox" id="topic_marketflash" value="marketflash" name="topic" />
Marketflash
</label>
<label for="topic_fuzz">
<input type="checkbox" id="topic_fuzz" value="fuzz" name="topic" />
Latest fuzz
</label>
<label for="topic_digester">
<input type="checkbox" id="topic_digester" value="digester" name="topic" />
Mailing list digester
</label>
<label for="topic" class="error">Please select at least two topics you'd like to receive.</label>
</fieldset>
<p class="btns">
<input type="submit" value="Submit"/>
</p>
</fieldset>
</form>

validate(options)的可选配置项:

debug:

进行调试模式(表单不提交): $(".selector").validate({debug:true});

把调试设置为默认: $.validator.setDefaults({debug:true});

submitHandler:
通过验证后运行的函数,里面要加上表单提交的函数,否则表单不会提交

$(".selector").validate({
   submitHandler:function(form) {
       $(form).ajaxSubmit();
   }
});

ignore:
对某些元素不进行验证 $("#myform").validate({
   ignore:".ignore"
});

rules:
自定义规则,key:value的形式,key是要验证的元素,value可以是字符串或对象

$(".selector").validate({
   rules:{
     name:"required",
     email:{
       required:true,
       email:true
     }
   }
});

messages:
自定义的提示信息key:value的形式key是要验证的元素,值是字符串或函数

$(".selector").validate({
   rules:{
     name:"required",
     email:{
       required:true,
       email:true
     }
   },
   messages:{
     name:"Name不能为空",
     email:{
       required:"E-mail不能为空",
       email:"E-mail地址不正确"
     }
   }
});

groups:
对一组元素的验证,用一个错误提示,用error Placement控制把出错信息放在哪里

$("#myform").validate({
groups:{
    username:"fname lname"
},
errorPlacement:function(error,element) {
     if (element.attr("name") == "fname" || element.attr("name") == "lname")
       error.insertAfter("#lastname");
     else
       error.insertAfter(element);
   },
   debug:true
});

Onubmit Boolean 默认:true
是否提交时验证 $(".selector").validate({
   onsubmit:false
});

onfocusout Boolean 默认:true
是否在获取焦点时验证

$(".selector").validate({
   onfocusout:false
});
onkeyup Boolean 默认:true
是否在敲击键盘时验证

$(".selector").validate({
   onkeyup:false
});
onclick Boolean 默认:true
是否在鼠标点击时验证(一般验证checkbox,radiobox)

$(".selector").validate({
   onclick:false
});
focusInvalid Boolean 默认:true
提交表单后,未通过验证的表单(第一个或提交之前获得焦点的未通过验证的表单)会获得焦点

$(".selector").validate({
   focusInvalid:false
});
focusCleanup Boolean 默认:false
当未通过验证的元素获得焦点时,并移除错误提示(避免和 focusInvalid.一起使用)

$(".selector").validate({
   focusCleanup:true
});
errorClass String 默认:"error"
指定错误提示的css类名,可以自定义错误提示的样式

$(".selector").validate({
   errorClass:"invalid"
});
errorElement String 默认:"label"
使用什么标签标记错误 $(".selector").validate
   errorElement:"em"
});
wrapper String
使用什么标签再把上边的errorELement包起来

$(".selector").validate({
   wrapper:"li"
});
errorLabelContainer Selector
把错误信息统一放在一个容器里面

$("#myform").validate({
   errorLabelContainer:"#messageBox",
   wrapper:"li",
   submitHandler:function() { alert("Submitted!") }
});

showErrors:
跟一个函数,可以显示总共有多少个未通过验证的元素

$(".selector").validate({
   showErrors:function(errorMap,errorList) {
        $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors,see details below.");
        this.defaultShowErrors();
   }
});
errorPlacement:
跟一个函数,可以自定义错误放到哪里

$("#myform").validate({
rrorPlacement:function(error,element) { error.appendTo(element.parent("td").next("td"));
   },
   debug:true

});
success:
要验证的元素通过验证后的动作,如果跟一个字符串,会当做一个css类,也可跟一个函数

$("#myform").validate({
        success:"valid",
        submitHandler:function() { alert("Submitted!") }
});
highlight:
可以给未通过验证的元素加效果,闪烁等


addMethod(name,method,message)方法:
参数name是添加的方法的名字
参数method是一个函数,接收三个参数(value,element,param) value是元素的值,element是元素本身 param是参数,我们可以用addMethod来添加除built-in Validation methods之

外的验证方法 比如有一个字段,只能输一个字母,范围是a-f,写法如下:

$.validator.addMethod("af",function(value,element,params){
   if(value.length>1){
    return false;
   }
   if(value>=params[0] && value<=params[1]){
    return true;
   }else{
    return false;
   }
},"必须是一个字母,且a-f");
用的时候,比如有个表单字段的id="username",则在rules中写
username:{
   af:["a","f"]
}

addMethod的第一个参数,就是添加的验证方法的名子,这时是af
addMethod的第三个参数,就是自定义的错误提示,这里的提示为:"必须是一个字母,且a-f"
addMethod的第二个参数,是一个函数,这个比较重要,决定了用这个验证方法时的写法
如果只有一个参数,直接写,如果af:"a",那么a就是这个唯一的参数,如果多个参数,用在[]里,用逗号分开

posted on 2012-02-17 10:33  封Ja葬va  阅读(583)  评论(0编辑  收藏  举报