2

I have date fields in my application, we got date pickers out but user is also allowed to enter the date in YYYY/MM/DD format. Now, we have handled the validations related to date format and invalid dates using the below regex:

"date": /^(?!0{4})\d{4}(\/)(0[1-9]|1[012])(\/)(0[1-9]|1[0-9]|2[0-9]|3[01])$/i,

Now, we are getting validations issue like

June is having 30 days but user is entering 31 and it is not getting validated Feb is having 28 days but user is entering 29 or above and it is not getting validated

One more scenario can come for feb month on leap year which needs to be validated

Can i use this scenarios to be handled by regex update or jquery code snippets??

Thanks in advance!!!

Salman A
  • 262,204
  • 82
  • 430
  • 521
GOK
  • 2,338
  • 6
  • 34
  • 63
  • first check if date is valid or not (this can be done by simply converting input into date object) and then check if the format is correct or not. – Nitin Agrawal Jun 17 '13 at 07:25
  • http://stackoverflow.com/questions/14691664/problems-using-javascript-regex-to-validate-date – Salman A Jun 17 '13 at 07:51

1 Answers1

2

Try this plugin: http://www.datejs.com/. It'll take care of everything for you. Date.parse('2013/06/31') returns null as expected.

tb11
  • 3,056
  • 21
  • 29