当前位置: 代码迷 >> Web前端 >> jQuery validate 日期比较 自定义应验方法
  详细解决方案

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

热度:133   发布时间:2013-01-11 11:57:35.0
jQuery validate 日期比较 自定义验证方法
来源:http://www.cnblogs.com/aaa6818162/archive/2012/03/06/2381414.html

<!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>
  相关解决方案