By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

If Statement help

P: 1
I need help (I'm a newby and just learning Javascript). I need an if statement for my required Address Fields that would only be required when "Other Address" radio button is selected.

Expand|Select|Wrap|Line Numbers
  1.     var x=document.forms["myForm"]["Addr0"].value
  2.     if (x==null || x=="")
  3.   {
  4.       alert("No/Street must be filled out");
  5.       return false;
  6.   }
  7.     var x=document.forms["myForm"]["Addr1"].value
  8.     if (x==null || x=="")
  9.   {
  10.       alert("Suburb must be filled out");
  11.       return false;
  12.   }
  13.     var x=document.forms["myForm"]["Addr2"].value
  14.     if (x==null || x=="")
  15.   {
  16.       alert("Please Select a State");
  17.       return false; 
  18.   }
  19.     var x=document.forms["myForm"]["Addr3"].value
  20.     if (x==null || x=="")
  21.   {
  22.       alert("Post Code must be filled out");
  23.       return false;
  24.   }
  25. <div id="radBtnCont">
  26.            Home Address: <input type="radio" name="radBtn" value="home" />
  27.             Other Address:<input type="radio" name="radBtn" value="other" /><br /><br />
  28.  
  29.    </div>
  30.         <div id="addressCont">
  31.  
  32. <I><B>If Other Address:</I></B><br /><br />
  33.  
  34.           No/Street:  <input type="text" name="Addr0" />
  35.  
  36.      Suburb: <input type="text" name="Addr1" /><br /><br />
  37.  
  38.     State: <select name="Addr2">
  39.                 <option> Please Select
  40.     <option value="act">ACT</option>
  41.     <option value="nt">NT</option>
  42.                 <option value="nsw">NSW</option>
  43.     <option value="qld" >QLD</option>
  44.     <option value="sa">SA</option>
  45.     <option value="tas">TAS</option>
  46.                 <option value="wa">WA</option></select>
  47.  
  48.     Post Code:<input type="text" name="Addr3" /><br />
  49.                </div><br /><br />
Thanks Joan.
Mar 4 '11 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
in pseudo code
Expand|Select|Wrap|Line Numbers
  1. // I abbreviated HTML elements by <element_description>
  2. if (<other address radio>.checked && !<other address text field>.value)
  3.     error()
PS. (x==null || x=="") is redundant, because a form elementís value is never null and you make a non-typesafe comparison (i.e. x==null fires also when x is false, 0, "" or undefined)
Mar 4 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.