Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS not reading field value as blank

Status
Not open for further replies.

lahddah

Programmer
Jan 2, 2003
109
US
Hi, I'm trying to verify a set of fields where, if the first name is not blank, then the address must be filled out. This is the code I have now - the first 'if' statement works fine:
-----------------------------------------

function ValidForm() {
if (document.request.r_name.value == "" || document.request.r_address == "" || document.request.re_email.value == "") {
alert("You must enter please enter your name, address and email address.");
document.request.r_name.focus();
return false;
}
if (document.request.f1name.value != "" || document.request.f1address.value == "") {
alert("You must enter your f1-friend's address.");
document.request.f1address.focus();
return false;
}
else
return true;
}
---------------------------
Here's the form fields:

<DIV ALIGN=&quot;left&quot;>Friend's Name:<BR>
<INPUT NAME=&quot;f1name&quot; TYPE=&quot;TEXT&quot; SIZE=&quot;30&quot; MAXLENGTH=&quot;120&quot; VALUE=&quot;&quot;></DIV>
<DIV ALIGN=&quot;left&quot;>Friend's Address:<BR>
<INPUT NAME=&quot;f1address&quot; TYPE=&quot;text&quot; SIZE=&quot;30&quot; MAXLENGTH=&quot;200&quot;></DIV>

-------------------------------
the alet msg &quot;You must enter your f1-friend's address&quot; pops up regardless of whether or not I enter anything in the field. I only need it if the field (f1name) is not equal to blank.

Thanks in advance!


~ lahddah
 
you need to change this line:
if (document.request.f1name.value != &quot;&quot; || document.request.f1address.value == &quot;&quot;) {

to this:
if (document.request.f1name.value != &quot;&quot; && document.request.f1address.value == &quot;&quot;) {


-kaht

banghead.gif
 
You could also test the length of the field value:

Code:
function ValidForm() {
if (document.request.r_name.value.length == 0 || document.request.r_address.value.length == 0 || document.request.re_email.value.length == 0) {
    alert(&quot;You must enter please enter your name, address and email address.&quot;);
document.request.r_name.focus();
return false;
    }
if (document.request.f1name.value.length > 0 || document.request.f1address.value.length == 0) {
    alert(&quot;You must enter your f1-friend's address.&quot;);
    document.request.f1address.focus();
     return false;
    }
else
return true;
}

There's always a better way. The fun is trying to find it!
 
Oh, my goodness, kaht. Thank you! I've tried many different variations of the '&&', but I think I had the second part in () so maybe that's why it wasn't working.

I thought that '||' equals 'and', but apparently it equals 'or'. Now I'm feeling silly!

thanks, again!


~ lahddah
 
thank you, too, tviman. During all my troubleshooting I was actually trying to figure out how to work with the 'length' function to accomplish the desired results. I'll keep this in mind for the future.

thanks!


~ lahddah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top