I want to have a regex for a date format, Example: 01-Jan-2011
I have written ^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4} but does not check for all the valid dates like 50-AAA-2011 will also be considered as valid.
I am using Live Validation lib in javascript
My code
var sayDate = new LiveValidation('date');
sayDate.add( Validate.Format, { pattern: /^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}/i });
Please help!
Trying to validate a date format using regex is actually a lot more complex than it sounds. You can do basic validation quite easily, but there’s always that bit more to do to get it perfect.
I would suggest instead using either a date picker control, which can do the validation for you (and also gives you the bonus of a nice shiny user interface feature), or else one of the existing Javascript libraries which can handle dates for you.
For date picker controls, there are any number of jQuery options. For date handling libraries, I would suggest looking up date.js.
Hope that helps.