I am creating an HTML Form having radio buttons which are being generated dynamically (for which I am using ASP). This means that, as a programmer, I am not aware of the no. of radio buttons that will be present when the user submits the Form. In order to ensure that atleast one radio button is checked when the Form is submitted, I am doing the following validation:
<html>
<head>
<script language="JavaScript">
function checkRadio(){
var j
for(j=0;j<=document.form1.pcontact.length;++j){
if(document.form1.pcontact[j].checked==false){
alert("Please Choose A Primary Contact !!!"
return false
}
}
}
</script>
<form method=post name="form1" action="NextPage.asp" onSubmit="return checkRadio()">
<!-- Here comes the radio buttons whose names are pcontact-->
.............
Now if a user doesn't check one of the radio buttons, as expected, he is shown the message saying "Please Choose A Primary Contact" but what I find is even if the user has ckecked one of the radio buttons, still he is being shown the same alert message as a result of which the user is not allowed to proceed ahead. What's wrong with the above JavaScript validation? Please give me a concrete solution to overcome this problem.
Thanks,
Arpan
<html>
<head>
<script language="JavaScript">
function checkRadio(){
var j
for(j=0;j<=document.form1.pcontact.length;++j){
if(document.form1.pcontact[j].checked==false){
alert("Please Choose A Primary Contact !!!"
return false
}
}
}
</script>
<form method=post name="form1" action="NextPage.asp" onSubmit="return checkRadio()">
<!-- Here comes the radio buttons whose names are pcontact-->
.............
Now if a user doesn't check one of the radio buttons, as expected, he is shown the message saying "Please Choose A Primary Contact" but what I find is even if the user has ckecked one of the radio buttons, still he is being shown the same alert message as a result of which the user is not allowed to proceed ahead. What's wrong with the above JavaScript validation? Please give me a concrete solution to overcome this problem.
Thanks,
Arpan