使用JQuery Validate以dd / mm / yyyy格式验证日期 [英] Validate date in dd/mm/yyyy format using JQuery Validate

查看:973
本文介绍了使用JQuery Validate以dd / mm / yyyy格式验证日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在输入出生日期和死亡日期。需要验证

I am taking input of date of birth and date of death. Validation required


  1. 死亡日期应超过出生日期

  2. 日期格式为dd / mm / yyyy

  3. 日期小于或等于今天。

验证不不能按预期工作,无法弄清问题。请帮助。

Validate doesn't work as expected and can't figure out the problem. Please help.

小提琴代码

使用JS库


  1. 日历/ Datepicker的JQuery UI

  2. JQuery验证表单验证

  3. 验证库的其他方法

  1. JQuery UI for Calendar/Datepicker
  2. JQuery validate for form validation
  3. Additional methods for validation library

var today = new Date();
var authorValidator = $("#itemAuthorForm").validate({
rules : {
    dateOfBirth : {
        required : false,
        date : true,
        dateITA : true,
        dateLessThan : '#expiredDate'
    },
    expiredDate : {
        required : false,
        date : true,
        dateITA : true,
        dateGreaterThan : "#dateOfBirth"
    }
},
onfocusout : function(element) {
    if ($(element).val()) {
        $(element).valid();
    }
}
});
var dateOptionsDOE = {
maxDate : today,
dateFormat : "dd/mm/yy",
changeMonth : true,
changeYear : true,
onClose : function(selectedDate) {
    $("#dateOfBirth").datepicker("option", "maxDate", selectedDate);
}
};
var dateOptionsDOB = {
maxDate : today,
dateFormat : "dd/mm/yy",
changeMonth : true,
changeYear : true,
onClose : function(selectedDate) {
    $("#expiredDate").datepicker("option", "minDate", selectedDate);
}
 };

jQuery.validator.addMethod("dateGreaterThan",
function(value, element, params) {
if ($(params).val() === "")
    return true;

if (!/Invalid|NaN/.test(new Date(value))) {
    return new Date(value) > new Date($(params).val());
}
return isNaN(value) && isNaN($(params).val())
        || (Number(value) > Number($(params).val()));
        }, 'Must be greater than {0}.');
        jQuery.validator.addMethod("dateLessThan",
        function(value, element, params) {
if ($(params).val() === "")
    return true;

if (!/Invalid|NaN/.test(new Date(value))) {
    return new Date(value) < new Date($(params).val());
}

return isNaN(value) && isNaN($(params).val())
        || (Number(value) < Number($(params).val()));
        }, 'Must be less than {0}.');
        $("#expiredDate").datepicker(
    $.extend({}, $.datepicker.regional['en-GB'], dateOptionsDOE));
        $("#dateOfBirth").datepicker(
    $.extend({}, $.datepicker.regional['en-GB'], dateOptionsDOB));



推荐答案

您不需要 date 验证器。它不支持dd / mm / yyyy格式,这就是为什么你得到请输入有效日期消息,如13/01/2014。您已经有 dateITA 验证器,根据需要使用dd / mm / yyyy格式。

You don't need the date validator. It doesn't support dd/mm/yyyy format, and that's why you are getting "Please enter a valid date" message for input like 13/01/2014. You already have the dateITA validator, which uses dd/mm/yyyy format as you need.

就像日期验证器,您的代码 dateGreaterThan dateLessThan code> new Date 用于输入字符串,并且具有相同的问题解析日期。您可以使用这样的函数来解析日期:

Just like the date validator, your code for dateGreaterThan and dateLessThan calls new Date for input string and has the same issue parsing dates. You can use a function like this to parse the date:

function parseDMY(value) {
    var date = value.split("/");
    var d = parseInt(date[0], 10),
        m = parseInt(date[1], 10),
        y = parseInt(date[2], 10);
    return new Date(y, m - 1, d);
}

这篇关于使用JQuery Validate以dd / mm / yyyy格式验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆