开发者

Jquery date validation bug

开发者 https://www.devze.com 2023-03-11 09:51 出处:网络
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) i

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 !


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"
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号