Click to See Complete Forum and Search --> : Javascript Error


Force
07-23-2003, 09:08 AM
I'm trying to use the following code to validate a form upon submission. The first two fields that I validated worked fine. When I added the last field to validate I messed something up, because it blows right through the validation, even if one of the first two is missing.
I think it's something in how I'm nesting the else statements, but I can't find the fix. I want to add numerous else statements to this.
I appeciate the help. Thanks.

<script language="JavaScript">
function doublecheck(){
if (document.myform.fname.value == "")
{
alert('First Name is a required field');
myform.fname.focus();
myform.fname.select();
return false;
}else{
if (document.myform.lname.value == "")
{
alert('Last Name is a required field');
myform.lname.focus();
myform.lname.select();
return false;
}
}else{
if (document.myform.phone.value == "")
{
alert('Phone is a required field');
myform.phone.focus();
myform.phone.select();
return false;
}
}
}

</script>

requestcode
07-23-2003, 10:16 AM
Try this:
if (document.myform.fname.value == "")
{
alert('First Name is a required field');
myform.fname.focus();
myform.fname.select();
return false;
}
else
{
if (document.myform.lname.value == "")
{
alert('Last Name is a required field');
myform.lname.focus();
myform.lname.select();
return false;
}
else
{
if (document.myform.phone.value == "")
{
alert('Phone is a required field');
myform.phone.focus();
myform.phone.select();
return false;
}
}
}

You had an extrac bracket "}" after the second if that was causing the problem.

Force
07-23-2003, 11:25 AM
Super,

Thanks for the assist.

Mark