|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Validating Radio Buttons
Does anyone know how to validate a selection of two radio buttons. But not a IF statement for just the two.
Meaning if later on there were more radio button options added the code wouldn't need fixed just the readio button? Thanks |
|
#2
|
||||
|
||||
|
Code:
<html>
<head>
<script language="JavaScript">
//this function takes two parameters, one, the radio group name and
//the group name or any meaningful message in quotes in the function call.
//you can omit the latter if you don't want the generic function to
//tell you which group was missing a button clicked
function validateRadio(group,grpname)
{
for (var i=0; i < group.length; i++)
{
if (group[i].checked)
{ i = group.length;
return true;
}
}
alert("No Radio Button Was Clicked On Group " + grpname + "\nMake Sure you Click At Least One");
return false;
}
function validate()
{
var group1 = document.forms[0].elements("Group1");
var group2 = document.forms[0].elements("Group2");
if (validateRadio(group1,"Group1") && validateRadio(group2,"Group2"))
return true;
else
return false;
}
</script>
</head>
<body>
<form action="tst.asp" method="post" onSubmit="return validate();">
<input type="radio" name="Group1" value="1">
<input type="radio" name="Group1" value="2">
<input type="radio" name="Group1" value="3">
<br>
<input type="radio" name="Group2" value="1">
<input type="radio" name="Group2" value="2">
<input type="radio" name="Group2" value="3">
<input type="submit" value="Submit">
</form>
</body>
</html>
Above is an example of a generic radio button check function. If you need to add more validation, you need to add more to the validate function for other radio button groups you may need. Feel free to post any additional questions you may have
__________________
................... ASCII and ye shall receive .................. Knowledge is the only resource on earth that multiplies when shared Support the Shemzilla Project Powered by C# |
|
#3
|
||||
|
||||
|
bears13,
In the future, try doing a search first, before posting a question. If you had searched for "Validating Radio Buttons" you would have found 9 different threads, that are similar to your question. |
|
#4
|
||||
|
||||
|
...and this is html/javascript issue - moved to the HTML forum.
|
|
#5
|
|||
|
|||
|
I should have been more specific. But I'm writing in vbscript. I have the logic just dont have the idea to write it
|
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Validating Radio Buttons |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|