Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dateISO bug: invalid month and date will pass the check #276

Closed
whuhacker opened this issue Dec 8, 2011 · 6 comments
Closed

dateISO bug: invalid month and date will pass the check #276

whuhacker opened this issue Dec 8, 2011 · 6 comments

Comments

@whuhacker
Copy link

For example, "2011-13-32" will pass the input validation. However, 13 is not a valid month, and 32 is not a valid date.

@knishant
Copy link

knishant commented Feb 9, 2012

Will something like the following fix the issue.

//dateISOF (f for fixed)
jQuery.validator.addMethod("dateISOF", function (value, element)
{
    if (this.optional(element))
    {
        return true;
    }
    if (!(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value)))
    {
        return false;
    }
    //fix starts here
    var split = value.replace(/\//g, "-").split("-");
    var year = parseInt(split[0]);
    var month = parseInt(split[1]) - 1;
    var date = parseInt(split[2]);
    var dateObj = new Date(year, month, date, 0, 0, 0, 0);
    return dateObj.getFullYear() == year && dateObj.getMonth() == month && dateObj.getDate() == date;
}, "Please enter a valid date (ISO).");

@hallberg
Copy link

I tried your fix and found an error in Chrome and Firefox (but it works in IE). Inputs like 1980-08-01 or 1980-11-09 does not pass the validation. The reason is that javascript returns zero for 08 and 09 when using parseInt. To fix this, add a second parameter to parseInt:

var month = parseInt(split[1], 10) - 1;
var date = parseInt(split[2], 10);

@knishant
Copy link

thanks for the fix.

regards,
Nishant

On Thursday 29 March 2012 01:42 AM, hallberg wrote:

I tried your fix and found an error in Chrome and Firefox (but it works in IE). Inputs like 1980-08-01 or 1980-11-09 does not pass the validation. The reason is that javascript returns zero for 08 and 09 when using parseInt. To fix this, add a second parameter to parseInt:

var month = parseInt(split[1], 10) - 1;
var date = parseInt(split[2], 10);


Reply to this email directly or view it on GitHub:
#276 (comment)

@vikaskanani
Copy link

View my post where I added some more validations and solves some of the issues.

@jzaefferer
Copy link
Collaborator

The dateISO method does only structure checks. This will get fixed with #581.

@jzaefferer
Copy link
Collaborator

Until then, using @vikaskanani's method override is certainly a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants