JQuery validate日期比较实例

发布时间:2020-07-30编辑:脚本学堂
本文介绍了jquery validate实现日期比较的方法,JQuery validate用法实例,感兴趣的朋友可以参考下。

例子,jquery validate应用实例。
 

复制代码 代码示例:
$(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("#__edit-form").validate({
            focusInvalid:false,
            rules:{
                "orgCreateDate":{
                    required: false
                },
                "orgCancelDate": {
                    required: false,
                    compareDate: "#orgCreateDate"
                }
            },
            messages:{
                "orgCreateDate":{
                    required: "成立日不能为空!"
                },
                "orgCancelDate":{
                    required: "注销日期不能为空!",
                    compareDate: "注销日期必须大于成立日期!"
                }
            }
        }); */ www.jb200.com
});

2.

复制代码 代码示例:

$(function(){
    $.validator.addMethod(
            "compareDate",
            function (value, element) {
            var startDate = $('#orgCreateDate').val();
return new Date(Date.parse(startDate.replace("-", "/"))) <= new Date(Date.parse(value.replace("-", "/")));
            },
            "结束日期必须大于开始日期"
    );

});

调用方法:
 

复制代码 代码示例:
<input type="text" id="orgCancelDate" name="orgCancelDate" size="10" readonly="true" validate="{required:false,compareDate:true,maxlength:12}" value="<fmt:formatDate value="${org.orgCancelDate}" pattern="yyyy/MM/dd"/>" />