首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

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

2012-10-10 
jQuery validate 日期比较 自定义验证方法!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN

jQuery validate 日期比较 自定义验证方法
<!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>

热点排行