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.
LVL 2
bloodtrainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.