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

Radio Button Validation

mageswar005
P: 72
I need a radio button empty validation,My radio button is in array .

for example: <input type="radio" name="material[]" >
Sep 24 '08 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
Use document.getElementsByName("material[]") to get the list of radio buttons. Loop over them checking the checked property of each and if none are checked, the validation would fail.
Sep 24 '08 #2

P: 93
This is the thing I think you wanted and Acoder has suggested.

[HTML]<html><body >
<td><input type="button" id ="floatButt" value="Click Me.." onclick="checkRadio();"/></td>
<td><input type="radio" id ="attachment1" name="radio"/></td>
<td><input type="radio" id ="attachment2" name="radio"/></td>

</body></html>[/HTML]
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function checkRadio(){
  3.  
  4. var radios=document.getElementsByName("radio");
  5. var radioLength=radios.length;
  6. for (i=0;i<radioLength;i++){var rad=radios[i];
  7. if(rad.checked==true){alert(rad.id);}
  8.  
  9.  
  10. }
  11.  
  12. </script>
This problem could arise..........

If suppose I want ids of all the radio in my page which is selected.It may be possible that there is more than 1 set of radios in my page one for gender another for marital status.And because of some other reason I have to keep the name not same.

Also If you want to write external js file which should work for any document

Then this is the solution.........

Expand|Select|Wrap|Line Numbers
  1. var alltags=document.getElementsByTagName("*");
  2. var tagLength=alltags.length;
  3.  
  4.     for (i=0;i<tagLength;i++)
  5.     {
  6.  
  7.         if(alltags[i].type=='radio')
  8.         {
  9.  
  10.             if(alltags[i].checked==true){alert(alltags[i].id);}
  11.  
  12.         }
  13.     }
Sep 25 '08 #3

Post your reply

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