It is very important to validate the data supplied by the user through a form before you process it. In the following examples, a Java Script function is used to check a valid date format against a regular expression.Among various kind of data validation, validation of date is one. Later we take each part of the string supplied by user (i.e.The code behind the form now is as follows: If you're not already familiar with regular expressions, then this might be getting a bit complicated.
If you're setting up a calendar of future events you would check that the year is the current year or greater.EDIT Note that the above regex is not correct (ie it can approve invalid dates). For instance, how do you handle leap years, different months having different number of possible days, etc. Use a library that can parse it, if it cant be parsed, its not a date time. I guess the brief answer is, don't use regex to validate date times. It does not check for leap years or invalid dates at the end of short months.As we've seen before, creating re-usable functions can significantly reduce the size of your Java Script code.