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

Radio Button validation before submit - did worked first time!?

Status
Not open for further replies.

hpadwal

Programmer
Feb 6, 2006
57
GB
hello all,

i have used this radio button validation before and it has workied, but isnt working on my new form.

can you help?

Here is my form head

Code:
<form name="form" method="post" onsubmit="return valformPage2(form);" action="default2.asp?id=<%=id%>&ref=<%=ref%>">

And the javaScript function
Code:
function valformPage2(form)
{
	myOption = 0;
	for (f=0; f<form.q0.length; f++) 
	{
	
		if (form.q0[f].checked) 
		{

			{		
				myOption = 1
			}
		}
	}

	if (myOption == 0) 
	{
		alert("You must select a option");
		return false;
	}
}

thank you

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

- Doh

 
For starters, I'd change this:

Code:
return valformPage2(form);

to this:

Code:
return valformPage2(this);

and this:

Code:
alert("You must select a option");

to this:

Code:
alert("You must select an option");

But some questions, as you seems to have omitted the most important bits of code!

- Do you have any radio buttons in your form named "q0"?

- Are there more than one of them?

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If you only have [!]one[/!] radio button named "q0", then why are you trying to loop round a control array if there isn't one?

This is why I asked the second question in my first post, which you did not answer (I was referring to radio buttons named "q0", not to all radio buttons).

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
hello,

Yes i did solveit, not sure if it was the best way but it goes like this>>

Code:
unction valformPage2(thisform)
{
	myOption = 0;
	
	
	for (f=0; f<thisform.q0.length; f++) 
	{
	if (thisform.q0[f].checked) 
		{
					
				myOption = myOption +  1
			
		}
	}
	
	for (f=0; f<thisform.q1.length; f++) 
	{
	if (thisform.q1[f].checked) 
		{
					
				myOption = myOption +  1
			
		}
	}
for (f=0; f<thisform.q1.length; f++) 
	{
	if (thisform.q1[f].checked) 
		{
					
				myOption = myOption +  1
			
		}
	}
	if (myOption == 3) 
	{
		thisform.submit();
	}
	else 
 	{
		alert("Please answer all the questions");
		return false;	
		
	}
}

Not sure if its the best way, definatly works and is a bit long winded.

Thanks for your help guys.



_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

- Doh

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top