• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2275
  • Last Modified:

Validating if a radio button is checked - not working!!

I'm using the function below to validate if a radio button has been checked. The function works fine with a form that contains multiple radio buttons but the value is undefned when there's just a single radio button.


<script language="javascript">
<!--
function getCheckedValue(radioObj) {
      var radioLength = radioObj.length;
      alert(radioLength);
      var ischecked = false;
      for(var i = 0; i < radioLength; i++)
      {
            if(radioObj[i].checked)
            {
                  ischecked = true;
                  document.frm.txt_recordid.value = radioObj[i].value;
                  document.frm.submit();
            }
      }
      if(ischecked == false){
            alert("You must first select a record.");
      }
}
//-->
</script>

<body>
...
<tr>
   <td width="25"><input name="rad_record" type="radio" value="<?php print $row["user_autoid"]; ?>"></td>
   <td width="395"><?php print $row["user_lname"] . ", " . $row["user_fname"]; ?></td>
</tr>
...
<input type="button" name="btn_edit" value="Edit" class="frm_button" onClick="getCheckedValue(document.frm.rad_record);">
...
</body>


Thanks.
0
bloodtrain
Asked:
bloodtrain
  • 2
1 Solution
 
gamebitsCommented:
Wouldn't a checkbox be more appropriate if you only have one option?

Gamebits
0
 
bubbledragonCommented:
radio will not create length property if you only one radio. so modify the code below will work for one radio.

Gamebits recommend checked is right.

If only one, the checkbox is best than radio. Radio cannot unchecked.

<script language="javascript">
<!--
function getCheckedValue(radioObj) {

     var ischecked = false;
     if (radioObj.length) {
           var radioLength = radioObj.length;
           for(var i = 0; i < radioLength; i++)
           {
                if(radioObj[i].checked)
                {
                     ischecked = true;
                   document.frm.txt_recordid.value = radioObj[i].value;
                }
           }
     } else if (radioObj.checked) {      
      ischecked = true;
      document.frm.txt_recordid.value = radioObj.value;
     }

     if(ischecked == true) {
      document.frm.submit();
     } else {
          alert("You must first select a record.");
     }
}
//-->
</script>
0
 
bloodtrainAuthor Commented:
gamebits:
The values are being pulled from a database so it might be 1 radio, 15 or 200.

bubbledragon:
I'll give your code a try - thanks.
0
 
bloodtrainAuthor Commented:
Works great.

Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now