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

how to validate radio button and drop down list using java script?

100+
P: 127
Form:

Expand|Select|Wrap|Line Numbers
  1. <form name="newuserForm" action="register.php" method="post" onsubmit="return validateForm()";>
  2.  
  3. <label style="font-size:18px">Gender<br />
  4. </label><label style="font-size:18px">Male </label><input id="male" type="radio" name="gender" class="box" style="border:solid 3px #333333" value="female" />
  5. <label style="font-size:18px">أنثى</label><input id="female" type="radio" name="gender" value="female"><br /><br />
  6.  
Java Script:
Expand|Select|Wrap|Line Numbers
  1. function validateForm()
  2. {
  3. if (document.getElementById("male").checked=false && document.getElementById("female").checked=false)
  4. {
  5.      alert("Please select Gender");
  6.   return false;
  7. }
  8. }
its not working? how to do it? please help....
Jun 17 '13 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
A single = is an assignment operator, while double == is a comparison operator. If you are checking whether two values match, you should use the double version.

Expand|Select|Wrap|Line Numbers
  1. // This is wrong. This will not check if it's false, it will
  2. // set it to false and then return true.
  3. if (value = false)
  4.  
  5. // This is how it should be.
  6. if (value == false)
  7.  
  8. // Or, since this is a boolean check, you can also do this.
  9. if (!value)
  10.  
Jun 17 '13 #2

Post your reply

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