Link to home
Start Free TrialLog in
Avatar of bloodtrain
bloodtrainFlag for Canada

asked on

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.
Avatar of gamebits
gamebits
Flag of Canada image

Wouldn't a checkbox be more appropriate if you only have one option?

Gamebits
ASKER CERTIFIED SOLUTION
Avatar of bubbledragon
bubbledragon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bloodtrain

ASKER

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.
Works great.

Thanks.