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.
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.
It is unit tested with 100% code coverage and can be considered fit for production.
You can find the details in Web Services using XMLHttp Request (Ajax) and related articles. It doesn't allow a space after minutes and before the am/pm example: " am" will get an invalid time. I modified the reg to the following: re = /^(\d): (\d)(:00)?
Instead we're going to make use of Form Validation using Ajax to do some real-time checking using a server-side PHP script to get a definitive answer.
When you enter a date in the format dd/mm/yyyy the value is sent via an Ajax call to the server where it is validated using the PHP checkdate function.
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.
Just be aware that it means more requests to the server which can be slower than downloading and running Java Script code.