jQuery validate 日期比较 自定义验证方法

来源:http://www.cnblogs.com/aaa6818162/archive/2012/03/06/2381414.html

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">

<HTML>

<HEAD>

<TITLE>NewDocument</TITLE>

<METAname="Generator"CONTENT="EditPlus">

<METAname="Author"CONTENT="">

<METAname="Keywords"CONTENT="">

<METAname="Description"CONTENT="">

<scripttype="text/javascript"src="jquery-1.4.2.js"></script>

<scripttype="text/javascript"src="jquery.validate.js"></script>

<SCRIPTLANGUAGE="JavaScript">

<!--

jQuery(function(){

jQuery.validator.methods.compareDate=function(value,element,param){

//varstartDate=jQuery(param).val()+":00";补全yyyy-MM-ddHH:mm:ss格式

//value=value+":00";

varstartDate=jQuery(param).val();

vardate1=newDate(Date.parse(startDate.replace("-","/")));

vardate2=newDate(Date.parse(value.replace("-","/")));

returndate1<date2;

};

jQuery("#form1").validate({

focusInvalid:false,

rules:{

"timeStart":{

required:true

},

"timeEnd":{

required:true,

compareDate:"#timeStart"

}

},

messages:{

"timeStart":{

required:"开始时间不能为空"

},

"timeEnd":{

required:"结束时间不能为空",

compareDate:"结束日期必须大于开始日期!"

}

}

});

});

//-->

</SCRIPT>

</HEAD>

<BODY>

<formid="form1"name="form1"method="post"action="demo.html">

<p>timeStart:

<inputname="timeStart"type="text"id="timeStart"value="2011-02-1710:00:00">yyyy-MM-ddHH:mm:ss

</p>

<p>timeEnd:

<inputname="timeEnd"type="text"id="timeEnd"value="2011-02-179:00:00">yyyy-MM-ddHH:mm:ss

</p>

<p>

<inputtype="submit"name="Submit"value="提交">

</p>

</form>

</BODY>

</HTML>

相关推荐