Regular expression for validating date javascript

6854933580_2c8b688306_z

When capturing information for insertion into a database, or use in other processing, it's important to control what the user can enter.

If you're setting up a calendar of future events you would check that the year is the current year or greater.

In this example, the date fields will only accept input that matches the pattern 'dd/mm/yyyy' (this could just as easily be changed to 'yyyy-mm-dd' or 'mm/dd/yyyy').

The time field will allow input starting with 'hh:mm' following by an optional 'am' or 'pm'. The code behind the form is as follows: For each field in the form (first the dates, then the time field), a check is made as to whether the input is blank.

We have also checked the leap year factor for the month of February.

We have used "/" and "-" character as a separator within the date format but you are free to change that separator the way you want.

Similar to this setup from another example: With Ajax you can make use of more powerful server-side functions and don't have to include large Java Script libraries for validating dates and other values.

You must have an account to comment. Please register or login here!