Click to See Complete Forum and Search --> : Validate checkboxes and radio using same function
yeannee
February 9th, 2009, 07:05 AM
Hi,
My form consists of 2 radio buttons and a list of checkboxes. At least one of the radio button and one of the checkboxes must be checked upon submission.
How to write a function in java script to validate radio and checkboxes based on the condition stated above?
Thanks in advanced.
HanneSThEGreaT
February 9th, 2009, 09:08 AM
To see how many Radio buttons are checked, you're going to need to loop through all the radio buttons on the page, then test its checked property, something like :
<SCRIPT LANGUAGE = "JavaScript">
function Clear(buttonGroup)
{
for (i=0; i < buttonGroup.length; i++) {
if (buttonGroup[i].checked == true) {
//Do further processing here, or example add a counter, then,
//based on that, you'll know how many wa selected.
}
}
}
</SCRIPT>
</HEAD>
<BODY>
Navigation Bar<P>
<INPUT TYPE = "radio" NAME = "LinkLoc" ID = "LinkLoc" VALUE = "specials">Specials.html
<INPUT TYPE = "radio" NAME = "LinkLoc" ID = "LinkLoc" VALUE = "contents">Contents.html
<INPUT TYPE = "button" NAME= "clear" VALUE ="clear" onClick="Clear(LinkLoc)"> This is the call to the above JS function..
</BODY>
</HTML>
That would hopefully get you started :)
PeejAvery
February 9th, 2009, 10:42 AM
Nice post, Hannes! :wave:
Don't forget that in JavaScript, booleans don't require the true declaration.
if (buttonGroup[i].checked) {
// this also works...and has been reported to process faster
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.