Jquery limit DOB fields to numbers
Is there a plugin for DOB fields t开发者_StackOverflowhat limits each field to only numbers and below a certain figure. eg I have there fields DD/MM/YYYY
I need the first field to accept only numbers up to 31 (no 32, no 62, etc), the second field to accept numbers no greater than 12 and the last to accept any numbers.
Thanks for any help.
Much appreciated.
It probably helps to know that this is called "validation".
Here you go. That page has a demo that you can use to test the plugin to make sure it does what you want.
And this one calls itself "better" date validation because it not only tests for 1-12, 1-31, etc, but also tests that the date is a valid date (not February 29 in the wrong year).
There is not a 100% trouble free solution to what you want to do. The problem is that text fields are designed to take any text input. When you try to override the behavior, you can only partially override, and in different ways on different browsers.
There are many complex issues here. Firstly, what if someone puts an invalid date, like 29th of february on a non leap year.
Also, where do you bind your validation rules? If you do it on keyup, the character goes in, then you validate and remove the character. If you do it on keydown, you have to try to prevent the default behavior, and then replace it with your own logic. What do you do if someone pastes with the right mouse button an invalid number?
Personally, I would use a html-5 style <input type="date" />
which allows people with modern browsers to take advantage of better input validation, without causing potential issues with unusual browsers/circumstances.
I think it is better to validate after the date has been submitted, or maybe on the date input losing focus rather than trying to block input.
精彩评论