bloodtrain
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(d ocument.fr m.rad_reco rd);">
...
</body>
Thanks.
<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.
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(d
...
</body>
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
Works great.
Thanks.
Thanks.
Gamebits