jQuery在多个文本字段中验证出生日期

jquery validate date of birth in multiple text fields

本文关键字:验证 出生日期 字段 文本 jQuery      更新时间:2023-09-26

我一直在寻找一种明确的方式来验证出生日期,当它被分成三个不同的文本字段时。到目前为止,我有一个正确处理它的输入字段,但我无法更改 html 代码。所以我仍然需要这个来遵守 3 文本字段组。我读到使用隐藏的输入字段来验证在三个字段中输入的出生日期值可能是一种解决方案。谁能帮我这个。

这是我的jsfiddle:http://jsfiddle.net/b8fubr5u/1/

$(document).ready(function () {
    $.validator.addMethod("dob", function (value, element) {
        var result = true;
        var ageMin = 18;
        var ageMax = 85;
        //is the date valid?
        //is it within the allowed range
        var myDate = value.split("/");
        var subDay = myDate[0];
        var subMonth = myDate[1] - 1;
        var subYear = myDate[2];
        var subDate = new Date(subYear, subMonth, subDay);
        // this will "correct" any out of range input
        var calcDay = subDate.getDate();
        var calcMonth = subDate.getMonth();
        var calcYear = subDate.getFullYear();
        // this checks to see if any of the submitted input was out of range
        // comment this out to ignore the discrepancy if you want to set a "corrected" value below
        if (calcDay != subDay || calcMonth != subMonth || calcYear != subYear) {
            $.validator.messages.dob = "Invalid date";
            result = false;
        }
        if (result) {
            var currDate = new Date();
            var currYear = currDate.getFullYear();
            var currMonth = currDate.getMonth();
            var currDay = currDate.getDate();
            var age = currYear - subYear;
            if (subMonth > currMonth) {
                age = age - 1; // next birthday not yet reached
            } else if (subMonth == currMonth && currDay < subDay) {
                age = age - 1;
            }
            if (ageMin != undefined) {
                if (age < ageMin) {
                    $.validator.messages.dob = "Min 18 years old";
                    result = false;
                }
            }
            if (ageMax != undefined) {
                if (age > ageMax) {
                    $.validator.messages.dob = "Invalid date";
                    result = false;
                }
            }
        }
        return result;
    },
        "Please enter a date in the format DD/MM/YYYY");
    $("#form").validate({
        rules: {
            dateBirth: {
                required: true,
                dob: true
            },
            month: {
                required: true,
                dob: true
            },
            dobDay: {
                required: true,
                range: [1, 31]
            },
            dobMonth: {
                required: true,
                range: [1, 12]
            },
            dobYear: {
                required: true
            }
        }
    });
});

你可以使用这个:

$(".bday").on('keyup',function(){
    var bday = $('[name="dobDay"]').val()+"/"+$('[name="dobMonth"]').val()+"/"+$('[name="dobYear"]').val();
    $("#dateBirth").val(bday );
});

这是小提琴的工作:http://jsfiddle.net/b8fubr5u/8/