Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts

    validate radio buttons

    I have 2 radio buttons

    In my validation javascript I do for example for a select box

    if (theForm.category.selectedIndex == 0)
    {
    alert("The first \"category\" option is not a valid selection. Please choose one of the other options.");
    theForm.category.focus();
    return (false);
    }

    How would I do the same to validate that my radio boxes are checked.
    For example I have 2 radio buttons called radio1 one with the value yes and one with the value no -- How do I validate that at least one was selected?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    There must be an easier way but

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>

    <
    script language="JavaScript" type="text/javascript">
    <!--
    var 
    ips,ckd;
    var 
    RadAry=new Array();
    var 
    GrpAry=new Array();

    function 
    CheckRadio(){
     
    ips=document.getElementsByTagName('INPUT');
     
    RadAry=new Array();
     for (
    i=0;i<ips.length;i++){
      if (
    ips[i].type=='radio'){
       
    RadAry[RadAry.length]=ips[i];
      }
     }
     
    GrpAry=new Array();
     
    GrpAry[0]=new Array();
     for (
    i2=0;i2<RadAry.length;i2++){
      
    GrpAry[GrpAry.length-1][GrpAry[GrpAry.length-1].length]=RadAry[i2];
      if (
    RadAry[i2+1]&&RadAry[i2+1].name!=RadAry[i2].name){
       
    GrpAry[GrpAry.length]=new Array();
      }
     }
     for (
    i3=0;i3<GrpAry.length;i3++){
      
    ckd=1;
      for (
    i4=0;i4<GrpAry[i3].length;i4++){
       if (
    GrpAry[i3][i4].checked==true){
        
    ckd=0
       
    }
      }
      if (
    ckd){
       
    alert('Radio Group '+GrpAry[i3][0].name+' is not checked');
      }
     }
    }

    //-->
    </script>
    </head>

    <body>


    <input type="radio" name="fred">
    <input type="radio" name="fred">
    <input type="radio" name="fred">
    <br>
    <input type="radio" name="tom">
    <input type="radio" name="tom">
    <input type="radio" name="tom">
    <br>
    <input type="button" name="" value="Check" onclick="javascript:CheckRadio();">
    </body>

    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:450px;left:0px;" >
    <input size=10 name=Show1 >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    </form>
    </html> 

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thanks.

    How can I change the checkradio function to return true if a radio button is checked and false if it not?

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Or you could just do a forum search for validate radio button, it's been answered numerous times

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Try this:
    Code:
    function checkRadio(radGrpName){
      var radios = document.theForm.elements[radGrpName];
      for (var i=0; i<radios.length; i++){
        if (radios[i].checked){
          return true;
        }
      }
      return false;
    }
    
    //sample usage using vwphillips' sample html
    if (!checkRadio("fred")){
      alert("Radio Group fred is not checked.");
      return false;
    }
    else if (!checkRadio("tom")){
      alert("Radio Group tom is not checked.");
      return false;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •