this link has validation function for radio
http://www.experts-exchang
Main Topics
Browse All Topicsokay i have the following question with four choices:
****************
Choose one of the following: <br />
<cfinput type="Radio" name="at" value="A"><font face="arial" size="2">choice A</font><br />
<cfinput type="Radio" name="at" value="B"><font face="arial" size="2">choice B</font><br />
<cfinput type="Radio" name="at" value="C"><font face="arial" size="2">choice C</font><br />
<cfinput type="Radio" name="at" value="D"><font face="arial" size="2">choice D</font><br />
***************
how do i make it so that the question requires one of the radio buttons to be selected?
and also how do i make one choice "pre-selected".
it seems that if i put a "required" attribute in each <cfinput.. then each one will have to be selected.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
this link has validation function for radio
http://www.experts-exchang
very simple article about "How to "
http://members.blue.net.au
so it will be like that
<HTML>
<HEAD>
<TITLE>TEST</TITLE>
<SCRIPT language = JavaScript>
<!--
function valbutton(thisform) {
// place any other field validations that you require here
// validate rb_at
myOption = -1;
for (i=0; i<thisform.rb_at.length; i++) {
if (thisform.rb_at[i].checked
myOption = i;
}
}
if (myOption == -1) {
alert("You must select a radio button");
return false;
}
// display the number of the button selected
else {
alert("You selected button number " + myOption
+ " which has a value of "
+ thisform.rb_at[myOption].v
return true;
}
// place any other field validations that you require here
}
//-->
</SCRIPT>
</HEAD>
<body>
<form name="myform" onsubmit="valbutton(this)"
Choose one of the following: <br />
<input type="Radio" name="rb_at" value="A"><font face="arial" size="2">choice A</font><br />
<input type="Radio" name="rb_at" value="B"><font face="arial" size="2">choice B</font><br />
<input type="Radio" name="rb_at" value="C"><font face="arial" size="2">choice C</font><br />
<input type="Radio" name="rb_at" value="D"><font face="arial" size="2">choice D</font><br />
<input type="submit" name="" value="Validate" />
<input type="reset" name="reset" value="Clear" />
</form>
</body>
</html>
You can use this function to check if any type of input has a value:
function hasValue(inputObj)
{
obj_type = inputObj.type.toUpperCase(
if (obj_type == "TEXT" || obj_type == "PASSWORD")
{ return (inputObj.value.length != 0); }
else if (obj_type == "SELECT-ONE")
{ return (inputObj.selectedIndex > 0); }
else if (obj_type == "SELECT-MULTIPLE")
{
for (var optionIndex=0; optionIndex < inputObj.length; optionIndex++)
if (inputObj.options[optionIn
{ return true; }
return false;
}
else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
{
if (inputObj.length)
{
for (var optionIndex=0; optionIndex < inputObj.length; optionIndex++)
if (inputObj[optionIndex].che
{ return true; }
return false;
}
else { return inputObj.checked; }
}
}
In your JavaScript, you can do this:
<form name="testForm" onsubmit="return validateForm">
Choose one of the following: <br />
<cfinput type="Radio" name="at" value="A" checked="true"><font face="arial" size="2">choice A</font><br />
<cfinput type="Radio" name="at" value="B"><font face="arial" size="2">choice B</font><br />
<cfinput type="Radio" name="at" value="C"><font face="arial" size="2">choice C</font><br />
<cfinput type="Radio" name="at" value="D"><font face="arial" size="2">choice D</font><br />
</form>
function validateForm()
{
if (!hasValue(at))
{
alert("You must select a radio value");
return false;
}
return true;
}
**************************
**************************
TO set one checkd by default, simply add the attribute checked="true" like I did above for the first option.
Hi, u wont need javascript if u use this !
pls chk the output of the following on the browser !
Choose one of the following: <br />
<cfform NAME="x" ACTION="akp.cfm">
<cfinput type="Radio" name="at" value="A" CHECKED><font face="arial" size="2">choice A</font><br/>
<cfinput type="Radio" name="at" value="B"><font face="arial" size="2">choice B</font><br/>
<cfinput type="Radio" name="at" value="C"><font face="arial" size="2">choice C</font><br/>
<cfinput type="Radio" name="at" value="D"><font face="arial" size="2">choice D</font><br/>
</CFFORM>
PS : no javascript required Since one is bydefault selected
& use will only be able to select
only one of them as they all have the same name !
K'Rgds
Anand
anandkp is correct. There really isn't much need for the JavaScript to validate that they selected a radio option if you have one set selected by default. The reason is that there is no way to de-select a radio option, so once one is selected, you can only change which option you have selected. So in setting a default selected option, the user will be gauranteed to have a radio option selected.
Business Accounts
Answer for Membership
by: HamdyHassanPosted on 2003-04-14 at 07:55:04ID: 8327117
you need javascript