Use regex.
var dateRegEx = /^(0[1-9]|1[012]|[1-9])[- /.](0[1-9]|[12][0-9]|3[01]|[1-9])[- /.](19|20)\d\d$/ console.log("06/06/2012".match(dateRegEx) !== null) // true console.log("6/6/2012".match(dateRegEx) !== null) // true console.log("6/30/2012".match(dateRegEx) !== null) // true console.log("30/06/2012".match(dateRegEx) !== null) // false
Learn about RegEx.
Edit - Disclaimer
As @elclanrs noted, this only confirms the format string, not the actual date, which means dates like the 31st will pass. However, since the OP only asks for βto check the format of the date string,β I will keep this answer here, because for some, this may be all you need.
As a side note, the jQuery validation plugin used by the OP also only validates the format.
Finally, for those who are wondering if you need to check the date, and not just the format, this regular expression will have a ~ 2% rejection rate in the domain (1-12) / (1-31) / (1900-2099 ) Please do not use this in the Critical Code for JPL.
source share