J
Jason & Juli Cook
The validation checks the firstname field fine and it checks the first date
field fine, but skips right over the second date field. Why? I think it has
something to do with line 15 (return (true). I put in the alerts just for
testing and it never reaches the alert("finished first date"); . . . . I'm
assuming because of the aforementioned return(true). How do I get the
validateForm function to continue checking the second date after the first
date is checked?
<html>
<head>
<title>Date Validation Test</title>
<script type="text/javascript">
function validateDate(str)
{
//Date format test from JavaScript Unleashed 4th ed.
var dateFormat = /^\d{1,2}\/\d{1,2}\/\d{4}/;
if(!dateFormat.test(str.value))
{
alert("The date is not in the correct format.");
str.focus();
return(false);
}
return(true); //problem?
}
function validateForm(myForm){
if (myForm.firstname.value==""||myForm.firstname.value==null){
alert("fix the firstname");
myForm.firstname.focus();
return(false);
}
alert("finished Name");
return validateDate(myForm.FirstDate);
alert("finished first date");
return validateDate(myForm.SecondDate);
alert("finished second date");
}
</script>
</head>
<body>
<h1>Multiple Date Validation</h1>
<form name="theForm" action= "DateVal.html?submitted=true" onSubmit="return
validateForm(document.theForm);">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" id="firstname" size="16"
maxlength="16"></td>
</tr>
<tr>
<td>First Date</td>
<td><input type="text" name="FirstDate" id="FirstDate" size="16"
maxlength="16"></td>
<td>( mm/dd/yyyy )</td>
</tr>
<tr>
<td>Second Date</td>
<td><input type="text" name="SecondDate" id="SecondDate" size="16"
maxlength="16"></td>
<td>( mm/dd/yyyy )</td>
</tr>
</table>
<input type="submit" value="Add">
</form>
</body>
</html>
field fine, but skips right over the second date field. Why? I think it has
something to do with line 15 (return (true). I put in the alerts just for
testing and it never reaches the alert("finished first date"); . . . . I'm
assuming because of the aforementioned return(true). How do I get the
validateForm function to continue checking the second date after the first
date is checked?
<html>
<head>
<title>Date Validation Test</title>
<script type="text/javascript">
function validateDate(str)
{
//Date format test from JavaScript Unleashed 4th ed.
var dateFormat = /^\d{1,2}\/\d{1,2}\/\d{4}/;
if(!dateFormat.test(str.value))
{
alert("The date is not in the correct format.");
str.focus();
return(false);
}
return(true); //problem?
}
function validateForm(myForm){
if (myForm.firstname.value==""||myForm.firstname.value==null){
alert("fix the firstname");
myForm.firstname.focus();
return(false);
}
alert("finished Name");
return validateDate(myForm.FirstDate);
alert("finished first date");
return validateDate(myForm.SecondDate);
alert("finished second date");
}
</script>
</head>
<body>
<h1>Multiple Date Validation</h1>
<form name="theForm" action= "DateVal.html?submitted=true" onSubmit="return
validateForm(document.theForm);">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" id="firstname" size="16"
maxlength="16"></td>
</tr>
<tr>
<td>First Date</td>
<td><input type="text" name="FirstDate" id="FirstDate" size="16"
maxlength="16"></td>
<td>( mm/dd/yyyy )</td>
</tr>
<tr>
<td>Second Date</td>
<td><input type="text" name="SecondDate" id="SecondDate" size="16"
maxlength="16"></td>
<td>( mm/dd/yyyy )</td>
</tr>
</table>
<input type="submit" value="Add">
</form>
</body>
</html>