...

View Full Version : Javascript IF OR problem



wolfenr
10-11-2011, 01:58 PM
Hi and hope someone can help.

I'm trying to put together some code within a form that tests whether someone has supplied an email address or a telephone number when the Submit button is pressed. Neither of these fields are require mandatory input.

Trouble is my current code always supplies a true result even if no data is put in these fields i.e. the 1st IF statement returns true. What am I doing wrong?

Here's my code.
Thanks
R
_____

<script type="text/javascript">
function check_id() {
if (document.feedback.email !="" || document.feedback.tel !="") {
alert("Thank you. You will be entered into our free prize draw. Good luck!");
} else if (document.feedback.email =="" && document.feedback.tel =="") {
confirm("You will not be entered in our free prize draw unless you supply your email address or telephone number. Is that O.K?");
}
}
function clear (){
var email="";
var tel="";
}
</script>

<form action="" method="get" name="feedback" enctype="application/x-www-form-urlencoded" onSubmit="check_id();" onReset="confirm('This will clear all your entries, is that O.K?'); clear();">
<div>
<label for="email">Your email address:</label>
<input name="email" type="text" id="email" size="50" value="" />
</div>
<div>
<label for="tel">Your telephone number:</label>
<input name="tel" type="text" id="tel" size="50" value="" />
</div>
<div>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</div>
</form>

niralsoni
10-11-2011, 02:43 PM
Add value property (as highlighted below) to compare with blank / empty string


if (document.feedback.email.value !="" || document.feedback.tel.value !="")

wolfenr
10-11-2011, 02:46 PM
Thanks for interest. I've now solved this, there were 2 issues:-

I needed to pass a true or false variable back from the check_id () function and this was missing.
I wasn't addresseing the correct value i.e. document.feedback.email instead of the correct statement (document.feedback.email.value)

Thanks again,
R


<script type="text/javascript">
function check_id() {
valid = true;
if (document.feedback.email.value !="" || document.feedback.tel.value !="") {
valid=true;
alert("Thank you. You will be entered into our free prize draw. Good luck!");
} else if (document.feedback.email.value =="" && document.feedback.tel.value =="")
{
valid=false;
confirm("You will not be entered in our free prize draw unless you supply your email address or telephone number. Is that O.K?");
}
return valid;
}
function clear (){
var email="";
var tel="";
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum