Description: This script validates a date field to ensure it's in the format mm/dd/yyyy.
It also intelligently checks that the date ranges are valid, so something like 02/30/2005 is caught.
The output will be almost identical to the earlier examples.
In this simple example we can even rewrite the In practice most modern browsers will now use HTML form validation to preempt any Java Script validation - with the notable exception of Safari. Visitors from some countries may find it confusing that we're using the and vice-versa as the year and day values have now changed position.
Each of these values is then tested against an allowed range (days: 1 - 31; months: 1 - 12; years: 1902 - 2018; and so on).
This script only confirms that the input format is correct and that each individual value falls within its allowed range.
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.
For example, when the date is checked, the return value, , is an array with elements 1 through 3 containing the day, month and year components of the input string.
For the time check, the array returned includes the hour (pos 1), minutes (pos 2) and, optionally, the am/pm string (pos 3).
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.
When capturing information for insertion into a database, or use in other processing, it's important to control what the user can enter.