We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

bloodtrain
bloodtrain asked
on
Medium Priority
2,306 Views
Last Modified: 2008-01-09
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.
Comment
Watch Question

CERTIFIED EXPERT

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

Gamebits
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>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.

Author

Commented:
Works great.

Thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.