first textbox works with script as it should but uses a submit button. Second textbox what I am trying to get to work and use script as you leave the textbox. I am using onBlur but not working. See code below. Validating date input - enter incorrect date so you can see error notification.
<HTML>
<HEAD>
<TITLE>Life Connection Program Survey</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// Also separates date into month, day, and year variables
var datePat = /^(d{1,2})(/|-)(d{1,2})2(d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true; // date is valid
}
// End -->
</script>
</HEAD>
<BODY background="lgrey011.jpg" onLoad="do_on_load()" Name="CONT">
<center>
<form onSubmit="return isValidDate(this.date.value);">
Program End Date: <input type=text name=date size=10 maxlength=10> (in MM/DD/YYYY format)
<input type=submit value="Submit Date">
</form>
</center>
<center>
<form name=LCPSurvey method=get action=LCPSurvey.php>
Program End Date: <input type=text name=date size=10 maxlength=10 onblur="isValidDate()"> (in MM/DD/YYYY format)
</form>
</center>
</BODY>
</HTML>
Software/Hardware used:
ASKED:
April 10, 2009 7:17 PM
UPDATED:
April 22, 2009 4:02 PM