Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Radio buttons problem

Status
Not open for further replies.

prakus

Programmer
Aug 4, 2003
19
US
I have the following code for validating Radio buttons:
When I select the radio button and submit, it gives me a message
'Please select a Section and press "Submit"'.
I donot have any problem when there are more than one radio buttons to choose from.

I am not able to figure out what the problem is. Any help will be appreciated.

Thank you.
PKS

function checkSecRadioAlert() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el.type == "radio") {
var radiogroup = el[el.name]; // get the whole set of radio buttons.
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please select a Section and press \"\Submit\"\.");
if(el.focus)
el.focus();
return false;
}
}
}
return true;
}

<Form name='RearrSecForm' Method='POST' Action='RearrangeVarFrame.asp' onsubmit='return checkSecRadioAlert(this);'>

<table>
<tr>
<td><input type='radio' value='1872' name='SecName'></td>
<td>Endowment</td> <tr>
<tr>
<td></td>
<td><input type='submit' value='Submit' name='Cnt'></td>
</tr>
</table>
</Form>
 
you need to check to see if you have more than one radio button first, using the existence of the "length" property of the radiobutton:

Code:
function checkSecRadioAlert() {
    var el = document.forms[0].elements;
        for(var i = 0 ; i < el.length ; ++i) {
            if(el[i].type == "radio") {
                var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
                var itemchecked = false;
                
                if (!radiogroup.length) {
                  itemchecked = radiogroup.checked;
                }
                else {
                  for(var j = 0 ; j < radiogroup.length ; ++j) {
                      if(radiogroup[j].checked) {
                          itemchecked = true;    
                          break;                    
                      }
                  }
                }
                if(!itemchecked) {
                    alert("Please select a Section and press \"\Submit\"\.");
                    if(el[i].focus)
                    el[i].focus();
                    return false;
                }                
            }
        }
    return true;
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Thank you Jeff. I should have noticed that.

Thank you very much.
PKS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top