当前位置: 代码迷 >> Web前端 >> jquery validate 札记
  详细解决方案

jquery validate 札记

热度:326   发布时间:2012-08-22 09:50:35.0
jquery validate 笔记

基础的参考:http://blog.csdn.net/zhoujiaolongde/article/details/6718565

?

jQuery.validator.addMethod("name",function,message)
1, name为验证规则的名称
2, function定义验证的规则。
3, message是return false时的提示信息。


其中 function(value, element, params) 定义:
1)element代表包含class有endDate的控件,example 1即id为stopD控件
2)value代表stopD控件的值
3)params代表传的值,比如example 3,"startDate","starth_v","endDate","endh_v"

?

example 1:
<input class="endDate" id="stopD">
js中定义:
jQuery.validator.addMethod("endDate",
?????? function(value, element) {
?????????? var startDate = $('#startD').val();
?????????? var d1=startDate.split("-");
?????????? var d2=value.split("-");
?????????? var day1=new Date(d1[0],d1[1],d1[2]);
?????????? var day2=new Date(d2[0],d2[1],d2[2]);
?????????? return day1 <= day2;
?????? },
?????? "End time must be greater than start time");

?

example 2:
jQuery.validator.addMethod("minWords", function(value, element, params) {
???? return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params;
?}, jQuery.validator.format("Please enter at least {0} words."));

?

example 3:
$("#queryForm").validate({
?rules: timecheck:{datediff: ["startDate","starth_v","endDate","endh_v"]}},
?messages:{timecheck:{datediff: "Start time must be less than end time"}}
?...
jQuery.validator.addMethod("datediff",
?function(value, element,param) {
?var d1=$("#"+param[0]).val();
?var h1=$("#"+param[1]).val();
?var d2=$("#"+param[2]).val();
?var h2=$("#"+param[3]).val();

...

  相关解决方案