Stupid Form Tricks
Dates and Calendars -
Validating and Formatting Dates
for most browsers
Nobody likes a nag. Not even when the nag is an HTML form. Most date validation scripts check character by character to see that the user has entered the date in the correct format. When it finds a problem, the nagging script alerts you to the problem and demands you fix it. This goes on and on until no problems are left. Well, I say the hell with that.
<form> Enter a Date (MM/DD/YY) : <input type="text" name="DATE1" size="10"> <input type="button" value="Test" onclick="checkDateFormat(this.form.DATE1,3)"> </form>
This script assumes the user is going to mess up. As long as you enter the
correct number of digits and put them in the correct order, the script will
check that the date is a valid using
function checkDateFormat(input1,formatNumber),
and if it is, it will put it in the correct format using
function dateFormats(month,day,year,formatNumber).
formatNumber corresponds to the formats defined in function
dateFormats().
0 = mmddyy
1 = mmddyyyy
2 = mm/dd/yyyy
3 = mm/dd/yy
4 = yyyymm
5 = ddmmyy
6 = dd/mm/yy
7 = ddmmyyyy
8 = dd/mm/yyyy
If you need a different date format, YYYY-MM-DD for instance, feel free to
modify the scripts to meet your needs. Just don't ask me to do it.
Validating and Comparing 2 Dates
function checkDateFormat() can take another text input as a 3rd argument - checkDateFormat(input1,formatNumber,input2)
When a second input is passed the function validates both date formats and checks that the first date is at least one day before the second.
<form> Start Date: <input type="text" name="START_DATE"> <br> End Date: <input type="text" name="END_DATE"><br> <input type="button" value="Test" onclick="checkDateFormat(this.form.START_DATE,6,this.form.END_DATE)"> </form>
You can copy the functions out of the textarea below:
PXL8 2004