Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    43
    Thanks
    18
    Thanked 1 Time in 1 Post

    Javascript IF OR problem

    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
    _____
    Code:
    <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>

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Add value property (as highlighted below) to compare with blank / empty string

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

  • Users who have thanked niralsoni for this post:

    wolfenr (10-11-2011)

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    43
    Thanks
    18
    Thanked 1 Time in 1 Post

    Solved!

    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

    Code:
      <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>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •