简体   繁体   English

使用validate.js比较日期

[英]Compare Dates using validate.js

in jquery i try to validate my page.I have to filds txtStartDate and txtEndDate .i want to validate start date is lower than end date.How this condition append in given code.Bcz txtPageName and txtTitle is already validate.Thanks. 在jquery中我尝试验证我的页面。我必须filds txtStartDatetxtEndDate 。我想验证开始日期是否低于结束日期。如果这个条件附加在给定的代码中txtPageNametxtTitle已经验证。 txtTitle

                 var v = $("#form1").validate({
                    ignore: ':hidden',
                    rules: {
                        : { required: true },
                        txtTitle: { required: true }


                    },
                    messages: {
                        txtPageName: "<br/>Please enter a Page Name",
                        txtTitle: "<br/>Please enter a Title"
                    }
                });

You can need a custom rule for doing this. 执行此操作可能需要自定义规则。 See the below code: isAfterStartDate checks is start date if less than end date and returns a boolean. 请参阅以下代码: isAfterStartDate检查是否小于结束日期的开始日期并返回布尔值。 This is used by the custom validation rule to validate the txtEndDate. 自定义验证规则使用此方法来验证txtEndDate。

var isAfterStartDate = function(startDateStr, endDateStr) {
            var inDate = new Date(startDateStr),
                eDate = new Date(endDateStr);

            if(inDate < eDate) {
                return false;
            }

        };
jQuery.validator.addMethod("isAfterStartDate", function(value, element) {

        return isAfterStartDate($('#txtStartDate').val(), value);
    }, "End date should be after start date");

 var v = $("#form1").validate({
                ignore: ':hidden',
                rules: {
                    txtPageName: { required: true },
                    txtTitle: { required: true },
                    txtStartDate : {required: true}
                    txtEndDate: {
                                  required: true,
                                  isAfterStartDate: true
                                 }


                },
                messages: {
                    txtPageName: "<br/>Please enter a Page Name",
                    txtTitle: "<br/>Please enter a Title"
                }
            });

This works assuming the date is format "dd/mm/yyyy". 假设日期格式为“dd / mm / yyyy”,则可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM