Radio Button Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mageswar005
    New Member
    • Mar 2008
    • 72

    Radio Button Validation

    I need a radio button empty validation,My radio button is in array .

    for example: <input type="radio" name="material[]" >
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use document.getEle mentsByName("ma terial[]") 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.

    Comment

    • Rsmastermind
      New Member
      • Sep 2008
      • 93

      #3
      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="checkR adio();"/></td>
      <td><input type="radio" id ="attachment 1" name="radio"/></td>
      <td><input type="radio" id ="attachment 2" name="radio"/></td>

      </body></html>[/HTML]
      Code:
      <script type='text/javascript'>
      function checkRadio(){
      
      var radios=document.getElementsByName("radio");
      var radioLength=radios.length;
      for (i=0;i<radioLength;i++){var rad=radios[i];
      if(rad.checked==true){alert(rad.id);}
      
      
      }
      
      </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....... ..

      Code:
      var alltags=document.getElementsByTagName("*");
      var tagLength=alltags.length;
      
      	for (i=0;i<tagLength;i++)
      	{
      
      		if(alltags[i].type=='radio')
      		{
      
      			if(alltags[i].checked==true){alert(alltags[i].id);}
      		
      		}
      	}

      Comment

      Working...