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

Validate Dynamic Radio Buttons 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
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=&quot;JavaScript&quot;>
function checkRadio(){
var j
for(j=0;j<=document.form1.pcontact.length;++j){
if(document.form1.pcontact[j].checked==false){
alert(&quot;Please Choose A Primary Contact !!!&quot;)
return false
}
}
}
</script>
<form method=post name=&quot;form1&quot; action=&quot;NextPage.asp&quot; onSubmit=&quot;return checkRadio()&quot;>
<!-- 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 &quot;Please Choose A Primary Contact&quot; 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
 
This should work with zero or more instances of the radio button:

<html>
<head>
<script language=&quot;JavaScript&quot;>
function checkRadio(){
var isChecked=false,r=document.form1.pcontact;
if(r && r.length>1){
for(var j=0;j<r.length;j++){
if(r[j].checked){isChecked=true;break;}
}
}
else if(r && r.checked){
isChecked=true;
}
if(r && !isChecked){
alert(&quot;Please Choose A Primary Contact !!!&quot;);
return false;
}
return true;
}
</script>
<form method=post name=&quot;form1&quot; action=&quot;NextPage.asp&quot; onSubmit=&quot;return checkRadio()&quot;>

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
I'm working in pretty much the same thing.
You have three cases to consider.
If the whole thing is dynamic (at least in my case)
1. You don't have anything to click.
2. You have only one checkbox/radio to click.
3. You have 2 or more.

What I'm using to trigger the function is a link that's why I send the name of the form I want to check.
BufID is the name of my radio/checkbox set created dynamically.

<script>
function checkSelected(txtForm){
var theForm=document.forms[txtForm];
var elemSelected=false;
var rptcount=0;
if(!theForm.bufid){alert('Nothing to Delete');return false;}

if(theForm.bufid.length){
for(var i=0;i<theForm.bufid.length;i++)
{if(theForm.bufid.checked){
rptcount++;
elemSelected=true;
}
}
}else{
if(theForm.bufid.checked){elemSelected=true;rptcount=1;}
}


if(elemSelected){
if(confirm(rptcount+' Reports selected for deletion. \n Do you want to continue?'))theForm.submit();
}else{
alert('Please select at least one Report to Delete');
return false;
}

return false;
}

</script>



on the form the trigger looks like
<a href=&quot;#&quot; onclick=&quot;javascript:checkSelected'nameofform');&quot;>Delete</a>


So far it seems to be working.

good luck.

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
As usual my index &quot;i&quot; is getting formatted as italics by tek-tips... sorry :)

<script>
function checkSelected(txtForm){
var theForm=document.forms[txtForm];
var elemSelected=false;
var rptcount=0;
if(!theForm.bufid){alert('Nothing to Delete');return false;}

if(theForm.bufid.length){
for(var index=0;index<theForm.bufid.length;index++)
{if(theForm.bufid[index].checked){
rptcount++;
elemSelected=true;
}
}
}else{
if(theForm.bufid.checked){elemSelected=true;rptcount=1;}
}


if(elemSelected){
if(confirm(rptcount+' Reports selected for deletion. \n Do you want to continue?'))theForm.submit();
}else{
alert('Please select at least one Report to Delete');
return false;
}

return false;
}

</script>





grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top