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

Radio Button Validation

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

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>

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function checkRadio(){
  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(;}
  10. }
  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;
  4.     for (i=0;i<tagLength;i++)
  5.     {
  7.         if(alltags[i].type=='radio')
  8.         {
  10.             if(alltags[i].checked==true){alert(alltags[i].id);}
  12.         }
  13.     }
Sep 25 '08 #3

Post your reply

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