...

View Full Version : function validate()



jwh500
05-06-2008, 06:20 AM
I am having a little trouble getting this function validate to work. I can get both if statements to work, just not at the same time. As it stands, just the fname and lname part works; however, if I remove that part, the address and email part will work. I would appreciate any help. Thanks.



function validate() {
if (document.form1.fname.value.length > 1
&& document.form1.lname.value. length > 1 )
return true;
else {
alert("Please enter First and Last Name.");
return false;}

if (document.form1.address.value.length > 1
|| document.form1.email.value. length > 1 )
return true;
else {
alert("Please enter Address or Email.");
return false;}
}

Here is the <form> attribute that I have.


<form name="form1" action="acknowledgement.html" method="get"
enctype="application/x-www-form-urlencoded"
onSubmit="return validate();">

tomws
05-06-2008, 07:12 AM
Logic problem. I think you're saying:

if ( fnameIsOk && lnameIsOkay )
return true; // I'm done - don't check anything else
else
return false; // and alert

if ( addressIsOk OR emailIsOk )
return true; // I'm done - don't check anything else
else
return false; // and alert


But what you want to say is:

if ( fnameIsNotOk )
return false; // and alert bad fname
else if ( lnameIsNotOk )
return false; // and alert bad lname
else if (addressIsNotOk && emailIsNotOk )
return false; // and alert bad address/email combo
else // all conditions passed
return true;


How does that look?

ghostz00
05-06-2008, 07:08 PM
When you use the return keyword (no matter what it returns) it stops the execution of the function. So when your first if statment returns true or false, the second if statement never gets executed.

I'd probably loop through all of the inputs and when there is no value, log it and then at the end return the errors.

jwh500
05-07-2008, 03:23 AM
Thanks tomws, You are right on. This was for an assignment at school, and I ended up doing it just as you suggested shortly after I posted here. (It was due in one hour) I figured it out with trial and error, but your posted showed the logic of it thanks a lot. I appreciate it. -Jeff



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum