Newbie form validation question

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>
 
L

Lee

Jason & Juli Cook said:
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");

}

Imagine a friend sends his young child to you to ask you to check
some facts. You tell the child to return with the message that
the facts are correct. If you then check something else and try
to tell the child to return with the results of that, too, you'll
find that you're talking to yourself, because the child left the
first time you told him to return.

Return means "return". Now. Anything a function says to do after
a return statement is just talking to itself.


You might get what you want with:

return(validateDate(myForm.FirstDate)&&validateDate(myForm.SecondDate));

That will run the first check and, if it is true, will also run the
second check, and if they're both true, will return "true". If either
check is false, the function will return "false".

Note that it won't make it clear to the user which date was wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top