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

Form validation and radio group focus

Status
Not open for further replies.

axsom1

Technical User
Feb 27, 2001
67
US
I have an online employment application I am creating for the office. In the application there are yes no questions that I have used radio buttons for. For some reason I can't get the script to focus on the radio group. Here is a simple snipet of what I am doing.

<script language=&quot;JavaScript&quot;>

function checkform() {

if (!document.form1.Gen_EighteenOrYounger.checked)
{
alert(&quot;Please select wether you are over 18 or not.&quot;);
document.form1.Gen_EighteenOrYounger.focus();
return false;
}

}
</script>

Now, I call this function by the onSubmit in the form tag.

<FORM method=&quot;POST&quot; action=&quot;review.asp&quot; name=&quot;form1&quot; onSubmit=&quot;return checkform()&quot;>

I get the alert to please select your age but then it continues on and processes the form and sends me over to the next page.

I don't get what I am missing. Is there an easier way to do this?

Thanks,
John Axsom
 
You can't refer to radio groups you have to use
their indexes:

<script language=&quot;JavaScript&quot;>
function checkform(form) {
var checked = false;
for(i=0;i < form.Gen_EighteenOrYounger.length; i++) {
if(form.Gen_EighteenOrYounger.checked) {
checked = true;}}
if (!checked)
{
alert(&quot;Please select wether you are over 18 or not.&quot;);
form.Gen_EighteenOrYounger[0].focus();
return false;
}
}
</script>

<FORM method=&quot;POST&quot; action=&quot;review.asp&quot; name=&quot;form1&quot; onsubmit=&quot;return checkform(this);&quot;>

Try it out....


2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top