Error checking with Radio buttons in Struts
I am trying to detect whether a radio button was selected before submitting the form
I want a popup alert saying "Please make a selection" if none of the radio buttons were selected.
heres what i have so far (i only created one button to see if it works):
<script type="text/JavaScript">
function checkRadio(){
var int i=0;
for(var i=0; i<aparValue.length; i++){
if(aparValue.checked)
return true();
}
alert('You must select a color!');
}
></script>
that was the checking method
and heres the radio button creation:
<bean:define id="aparValue">
<bean:write name="aparList" property="APAR"/>
</bean:define>
<html:radio property="APAR" value="<%=aparValue%>"></html:radio>
Since struts taglib doesn't allow radio "name" tag how can i point the javascript to check this specific radio button?
need some suggestions.
Thanks in advance. =)
[1006 byte] By [
Huija] at [2007-11-27 10:45:14]

# 1
View source on the created html page.
That will show you the "name" that is being created for the <input type="radio">
The name should probably show up as APAR
(any reason you are using all uppercase letters for properties? Its not really standard)
I would suggest having at least TWO radio options for this javascript code to work. Where are you getting the javascript variable aparValue from?
I am guessing something like document.forms[0].APAR
Javascript has a feature that when you reference a name in this fashion, if there is only one thing with that name, it will return that thing. If there are multiple components with that name, it returns an array of them.
So either your code should handle the 1 / many options or just assume that there will be many.
Hope this helps,
evnafets
# 2
Thanks for the help. I will give that a try now and post back what I get from it.
no reason why I am using capitals.
yes the loop in javascript is made so that it can handle multiple buttons. but for now I was trying to get one button to work then implement more. I was hoping the loop would stlil work with just one radio button. maybe i should change it to <= APAR.length.
I am pulling aparValue from a form bean. in the bean aparList with property APAR. its somewhat a hack to assigning aparValue with that property value. Found an example somewhere.
Thanks for the help =)
Huija at 2007-7-28 20:11:48 >

# 3
ok i checked view source and you were right, the name of the radio button is APAR
so heres another problem.
here is the javascript. I commented out the loop so i just checks the one button
<script type="text/JavaScript">
function checkRadio(){
//var int i=0;
//for(var i=0; i<=APAR.length; i++){
if(APAR.checked)
return true;
}
alert('You must make a selection!');
return false;
}
</script>
</HEAD>
this is down within the <head> tag.
in the body i have the form starting like this, and then a button for submit
<html:form action="aparDetailCmd.do" onsubmit="return checkRadio();">
<html:submit property="action" >Submit for CERT</html:submit>
won't "onsubmit" will take it to the javascript method checkRadio and show the alert?
Huija at 2007-7-28 20:11:48 >
