Carine
Carine

Reputation: 3

Jquery date validation bug

I've been trying to implement a jQuery function to validate date formats but I have encountered a curious problem. When the number '8' or '9' is contained in the day or month, (ex : 08/06/2011) it says the date is invalid...

A demo is available here : http://www.geektantra.com/projects/jquery-form-validate/advanced_demo/

Thanks for you help !

Upvotes: 0

Views: 892

Answers (1)

DarthJDG
DarthJDG

Reputation: 16591

When there are problems with the values 08 and 09, you almost certainly forgot to specify the radix parameter on parseInt().

If it's not specified, it tries to auto-detect the base from the input, and if it finds a leading zero, it tries to parse it as an octal number. For numbers 00 to 07 it's not an issue, it even evaluates to the same as if it was base-10, but for obvious reasons 08 and 09 are invalid octal numbers.

To avoid this issue in the future, make sure you always set the radix parameter to force parsing a number as base-10, regardless of the input:

var value = parseInt(someString, 10);

Check out your javascript around line 29 of your HTML file and change it to the following:

jQuery("#ValidDate").validate({
    expression: "if (!isValidDate(parseInt(VAL.split('-')[2], 10), parseInt(VAL.split('-')[0], 10), parseInt(VAL.split('-')[1], 10))) return false; else return true;",
    message: "Please enter a valid Date"
});

Upvotes: 4

Related Questions