Poppy
Poppy

Reputation: 3092

Jquery date validation for invalid date

i need to validate the date format for a particular date picker control. If I enter "99/99/9999" which is similar to the format DD/MM/YYYY manually, it does not throw up a validation error.

Is there any way to achieve this?

Upvotes: 0

Views: 3665

Answers (1)

elclanrs
elclanrs

Reputation: 94101

You can validate a date with this function. Just pass in your input's value. Extracted from this little validation plugin I'm working on.

var isValidDate = function (value) {
    var match = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.exec(value),
    isDate = function (m, d, y) {
        return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
    };
    return match && isDate(match[1], match[2], match[3]);
}

Upvotes: 1

Related Questions