how to validate Date of birth with three textbox of date month year by jquery validator

后端 未结 4 1926
情深已故
情深已故 2021-01-16 08:36

I need to validate date of birth by using jquery validator with three separate textbox for day month and year.

How to do this please help.

HTML code

相关标签:
4条回答
  • 2021-01-16 08:40

    You should create a custom validation method for this, along with using the groups option that validate provides:

    /* Custom validation method to validate a date based on several fields: */
    $.validator.addMethod("datemultiple", function(value, element, params) {
        var daySelector = params[0],
            monthSelector = params[1],
            yearSelector = params[2],
            day = parseInt($(daySelector).val(), 10),
            month = parseInt($(monthSelector).val(), 10),
            year = parseInt($(yearSelector).val(), 10),
            dateEntered = new Date(year, month - 1, day);
    
        return this.optional(element) || !isNaN(dateEntered.valueOf());
    
    }, "Please enter a valid date");
    
    $(document).ready(function() {
        $("#myform").validate({
            groups: {
                /* Only display one validation message for day, month, and year: */
                dateOfBirth: "dob-day dob-month dob-year"
            },
            rules: {
                'dob-day': {
                    required: true,
                    datemultiple: ["#dob-day", "#dob-month", "#dob-year"]
                },
                'dob-month': {
                    required: true
                }
            },
            /* Place error messages after the "year" field */
            errorPlacement: function ($error, $element) {
                if ($element.data("fieldgroup") === "dob") {
                    $error.insertAfter("#dob-year");
                }
            }
        });
    });
    

    Example: http://jsfiddle.net/xHC86/

    0 讨论(0)
  • 2021-01-16 08:49

    sorry, i'm cannot speak english

    you can check date < 30 or 31 respectively month month < 12 year ...

    i think it's easy when you used three textbox :( just js

    0 讨论(0)
  • 2021-01-16 08:53

    I wrote a Javascript module that handles whether or not the data is valid, you can check out a full working example in the JSFiddle link.

    http://jsfiddle.net/dceast/vmHjN/

    Here is the module that does the validation:

    var compareDate, checkDates = false;
    var validateObject = {
        init: function(year, month, day) {
            return this.compareDate.init(year, month, day);
        },
        compareDate: {
            init: function(year, month, day) {
                var isValid = false;
                // Compensate for zero based index, if month was not
                // subtracted from one 0 === Jan, 1 === Feb, 2 === Mar
                month -= 1;
    
                // Create a new date object with the selected
                // year, month, and day values and retrieve the
                // milliseconds from it.
                var mSeconds = (new Date(year, month, day)).getTime();
                var objDate = new Date();
    
                // Set the time of the object to the milliseconds 
                // retrieved from the original date. This will
                // convert it to a valid date.
                objDate.setTime(mSeconds);
    
                // Compare if the date has changed, if it has then
                // the date is not valid 
                if (objDate.getFullYear() === year &&
                    objDate.getMonth() === month &&
                    objDate.getDate() === day) 
                {
                    isValid = true;
                }
                return isValid;
            }
        }
    };
    
    0 讨论(0)
  • 2021-01-16 09:06
    var compareDate, checkDates = false;
    var validateObject = {
        init: function(year, month, day) {
            return this.compareDate.init(year, month, day);
        },
        compareDate: {
            init: function(year, month, day) {
                var isValid = false;
                // Compensate for zero based index, if month was not
                // subtracted from one 0 === Jan, 1 === Feb, 2 === Mar
                month -= 1;
    
                // Create a new date object with the selected
                // year, month, and day values and retrieve the
                // milliseconds from it.
                var mSeconds = (new Date(year, month, day)).getTime();
                var objDate = new Date();
    
                // Set the time of the object to the milliseconds
                // retrieved from the original date. This will
                // convert it to a valid date.
                objDate.setTime(mSeconds);
    
                // Compare if the date has changed, if it has then
                // the date is not valid
                if (objDate.getFullYear() === year&&
                    objDate.getMonth() === month &&
                    objDate.getDate() === day)
                {                
                    if(objDate <= new Date())
                    {
                        isValid = true;
                    }                   
                }
                return isValid;
            }
        }
    };
    
    $(function() {
        var validateButton = $('#btValidate');
    
        validateButton.click(function(e) {
            var month = parseInt(document.getElementById('month').value, 0),
                day = parseInt(document.getElementById('day').value, 0),
                year = parseInt(document.getElementById('year').value, 0),
                alertBox = $('#alert'),
                isValid = false;
    
            isValid = validateObject.init(year, month, day);
            var color, message;
    
            if (isValid === true)
            {
                color = "#008000";
                message = "Your date is valid!";
            }
            else if (isValid === false)
            {
                color = "#F00";
                message = "Your date is not valid!";
            }
            alertBox.css('background', "" + color)
                .html("<p>"+ message +"</p>")
                .stop()
                .animate({
                    width: "200px",
                    paddingLeft: "75px"
                }, 1750, "easeOutBounce", function() {
                    $(this).animate({
                        width: "0px",
                        paddingLeft: "0px"
                    }, 1000, "easeInBounce");
            });
        });
    });
    

    Working code here: http://jsfiddle.net/vmHjN/140/

    0 讨论(0)
提交回复
热议问题