跳转到内容


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


  • 您无法回复此主题
No replies to this topic

#1 冰力

    Administrator

  • 总版主
  • 1316 帖子数:

发表于 2011/02/24 15:55:42

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script type="text/javascript" src="jquery-1.4.2.js"></script>
  <script type="text/javascript" src="jquery.validate.js"></script>
  <script LANGUAGE="JavaScript">
  <!--
    jQuery(function(){        
        jQuery.validator.methods.compareDate = function(value, element, param) {
            //var startDate = jQuery(param).val() + ":00";补全yyyy-MM-dd HH:mm:ss格式
            //value = value + ":00";
            
            var startDate = jQuery(param).val();
            
            var date1 = new Date(Date.parse(startDate.replace("-", "/")));
            var date2 = new Date(Date.parse(value.replace("-", "/")));
            return date1 < date2;
        };
        
        jQuery("#form1").validate({
            focusInvalid:false,
            rules:{
                "timeStart":{
                    required: true
                },
                "timeEnd": {
                    required: true,
                    compareDate: "#timeStart"
                }
            },
            messages:{
                "timeStart":{
                    required: "开始时间不能为空"
                },
                "timeEnd":{
                    required: "结束时间不能为空",
                    compareDate: "结束日期必须大于开始日期!"
                }
            }
        });
    });
  //-->
  </SCRIPT>
 </HEAD>

 <BODY>
 <form id="form1" name="form1" method="post" action="demo.html">
   <p>timeStart:
     <input name="timeStart" type="text" id="timeStart" value="2011-02-17 10:00:00"> yyyy-MM-dd HH:mm:ss
   </p>
   <p>timeEnd:
     <input name="timeEnd" type="text" id="timeEnd" value="2011-02-17 9:00:00"> yyyy-MM-dd HH:mm:ss
   </p>
   <p>
     <input type="submit" name="Submit" value="提交">
   </p>
 </form>
  
 </BODY>
</HTML>