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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PLEASE HELP!! Radio button problem

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
0
0
US
Hello,

I have a radio button validation script that should display an error message if no button is selected. It displays the error correctly when the form is submitted without a button being checked, but it also displays when the button is checked. Can someone look this over and let me know what is wrong?

Thanks!
---------------------------------------------------------------------------------------------------
The Script:


<Script Language=&quot;Javascript&quot;>
<!-- Begin
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el.type == &quot;radio&quot;) {
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(&quot;Please choose a record to commit this action on.&quot;);
if(el.focus)
el.focus();
return false;
}
}
}
return true;
}
// End -->

</SCRIPT>


---------------------------------------------------------------------------------------------------
The Form:
<form action=&quot;pendingorders_RecordAction.cfm&quot; method=&quot;post&quot; name=&quot;menu&quot; onsubmit=&quot;return checkRadios(this);&quot;>

<p align=&quot;left&quot;>
<INPUT type=&quot;submit&quot; name=&quot;btnView_Delete&quot; value=&quot;Cancel Order&quot;>
<INPUT type=&quot;submit&quot; name=&quot;btn_View_Bill&quot; value=&quot;View Order&quot;>
<input type=&quot;submit&quot; name=&quot;btn_Edit_Bill&quot; value=&quot;Edit Order&quot;>
<input type=&quot;submit&quot; name=&quot;btnView_MainMenu&quot; value=&quot;Return to Main Menu&quot;>
<cfoutput><input type=&quot;hidden&quot; name=&quot;ID&quot; value=&quot;#URL.ID#&quot;</cfoutput>

</p>
<table border=0 cellpadding=3 cellspacing=0 size=&quot;600&quot;>
<tr>
<TD bgcolor=&quot;#000000&quot;><b><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot;></font></b></TD>
<TD bgcolor=&quot;#000000&quot;><b><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;2&quot;>Order ID No:</font></b></TD>
<TD bgcolor=&quot;#000000&quot;><b><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;2&quot;>Event Date:</font></b></TD>
<TD bgcolor=&quot;#000000&quot;><B><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;2&quot;>Date of Order:</font></B></TD>
<TD bgcolor=&quot;#000000&quot;><b><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;2&quot;>Total Order Cost:</font></b></TD>

<TD bgcolor=&quot;#000000&quot;><b><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;2&quot;>Select Record:</font></b></TD>
</tr>
<cfparam name=&quot;startrow&quot; default=&quot;1&quot;>
<cfparam name=&quot;maxrows&quot; default=&quot;10&quot;>
<cfoutput query=&quot;selectordersub&quot; maxrows=&quot;#maxrows#&quot; startrow=&quot;#startrow#&quot;>
<TR bgcolor=&quot;###Iif(((CurrentRow MOD 2) is 0),de('FFA8A8'),de('FFFFFF'))#&quot;>
<TD><font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;></font></TD>
<TD><font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>#ResultField1#</font></TD>
<TD><font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>#DateFormat(ResultField2, &quot;mm/dd/yy&quot;)# <input type=&quot;hidden&quot; name=&quot;eventdate&quot; value=&quot;#Dateformat(ResultField2,&quot;mm/dd/yy&quot;)#&quot;</font></TD>
<TD><font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>#DateFormat(ResultField3, &quot;mm/dd/yy&quot;)#</font></TD>
<TD><font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>#DollarFormat(ResultField6)#</font></TD>
<TD width=&quot;225&quot;><INPUT type=&quot;radio&quot; name=&quot;RecordID&quot; value=&quot;#ResultField1#&quot;></TD>
</tr>
</cfoutput>
<br>
<cfoutput>
<cfif startrow GTE maxrows>
<cfset startrow1 = #startrow# - maxrows>
<input type=&quot;Button&quot; value=&quot;<< Previous #maxrows# Records&quot; onclick=&quot;window.location='pendingorders.cfm?startrow=#startrow1#&ID=#URL.ID#'&quot;>
</cfif>
<cfset startrow = #startrow# + maxrows>
<cfif #selectordersub.recordcount# GTE startrow>
<input type=&quot;Button&quot; value=&quot;Next #maxrows# Records >>&quot; onclick=&quot;window.location='pendingorders.cfm?startrow=#startrow#&ID=#URL.ID#'&quot;>
</cfif>
</cfoutput>
<br>
<cfif #selectordersub.recordcount# EQ &quot;0&quot;>
<font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;3&quot; color=&quot;red&quot;><b>You do not have any current orders pending.</b></font>
</cfif>
<br>
<br>
</table>
<div align=&quot;center&quot;>
<p align=&quot;left&quot;></p>
</div>


</form>
 

for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}


Maybe you should look this block in the code.
The brackets in the code may be the problem.
Try to put break after if statement;then close for structure.Such this:

for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;

}
break;
}

I am not sure this will work but try once.
 
Change this line if(radiogroup[j].checked) to this:

if(eval(&quot;radiogroup[j].checked&quot;) == true))

There's always a better way...
 
Thanks for the suggestions, I really appreciate the help.

But I tried both suggestions and I still get errors. When I move the break statement I still get the same error.

When I changed the radio group statement, it stopped displaying the pop up all together.

Any other suggestions would be greatly appreciated or if anyone can suggest another way to write this statement.

 
nalbiso, OK strange enough I looked at your code and even copied/pasted into dreamweaver so I could see it in action, but with/without the radiobutton selected, I get NO ALERT MESSAGE...
I have not failed; I have merely found 100,000 different ways of not succeding...
 
Sorry,

I was moving stuff around trying to get this to work when I copied the Javascript. This one works:

<Script Language=&quot;Javascript&quot;>
<!-- Begin
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el.type == &quot;radio&quot;) {
var radiogroup = el[el.name]; // get the whole set of radio buttons.
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(eval(radiogroup[j].checked == true))
break;
}
}
if(!itemchecked) {
alert(&quot;Please choose a record to commit this action on.&quot;);
if(el.focus)
el.focus();
return false;
}
}
}
return true;
}
// End -->

</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top