Regular expression for validating date of birth
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.Basically, for each of the regular expression tests, an array is returned holding each component of the pattern that we've matched.In Java Script to check for different month lengths, particularly for February in leap years, you need quite a bit of extra code.I'm not going to show that here, but you can find a link to get started under References below. The year limitation would be used if you were asking for a date of birth or date of some recent event.
In this case, we've created a stand-alone functions which will validate a date field: In each case the value passed to the function is the form field rather than the field value.This routine DOES NOT check that the date or time input values are valid, just that they match the required format (d/m/y and h:m). Once you're in control of the input format, it's a lot easier to check that the values are actually valid.The function has been improved now so that the day, month and year values are checked to ensure that they're in the right ball-bark (ie. Also the year must be between 1902 and the current year.When capturing information for insertion into a database, or use in other processing, it's important to control what the user can enter.
Otherwise you can end up with values in the database that have no relation to reality.
It does not check for leap years or invalid dates at the end of short months.