Page 1 of 1

Radio Button Validation

Posted: Tue Jun 15, 2004 2:03 pm
by Kingo
I'm trying to validate if atleast one radio button is checked...There are 4 radio buttons. Form1 is the name of the form.

Code: Select all

if((Form1.Radio4.checked || Form1.Radio5.checked || Form1.Radio6.checked || Form1.Radio7.checked))			
{
    alert("Please enter Radio.");
    return false;
}
Any help is appreciated

edited by Weirdan: Please use

Code: Select all

tags for html fragments[/color]

No need for validation

Posted: Tue Jun 15, 2004 2:30 pm
by anjanesh
Client Side : Javascript
Yes. You can write a javascript function to validate if atleast one radio button in a GROUP is clicked. This is what I used to do before too but I found better method which does not require validation from our part.

Code: Select all

<input type="radio" ID="Radio1" VALUE="Radio1" NAME="RadioGroup"> Radio 1
<input type="radio" ID="Radio2" VALUE="Radio2" NAME="RadioGroup"> Radio 2
<input type="radio" ID="Radio3" VALUE="Radio3" NAME="RadioGroup"> Radio 3
<input type="radio" ID="Radio4" VALUE="Radio4" NAME="RadioGroup"> Radio 4
Here only one gets selected.

Posted: Tue Jun 15, 2004 2:43 pm
by Kingo
THe user should get a popo up box asking to select the radio button if in case he forgets....

Posted: Tue Jun 15, 2004 2:52 pm
by anjanesh
Set a default value :

Code: Select all

<input type="radio" ID="Radio1" VALUE="Radio1" NAME="RadioGroup" CHECKED> Radio 1

Posted: Tue Jun 15, 2004 2:54 pm
by Kingo
I donot want to set a default value.SO, trying to validate if the radio button is checked

Posted: Tue Jun 15, 2004 3:00 pm
by dull1554
well you cant do this with php, thats a client side function(a popup box) to validate with php you have to submit the entire page

Posted: Tue Jun 15, 2004 3:07 pm
by Kingo
I'm trying to do this with Java Script

Posted: Tue Jun 15, 2004 3:11 pm
by dull1554
like i said thats your best bet but best of luck anyways

Posted: Tue Jun 15, 2004 3:21 pm
by anjanesh

Code: Select all

<script language="javascript">
function ValidateRadios()
 &#123;
 	flag=false;
 	for (var i=1;i<=4;i++)
 	 &#123;
 	 	var el=document.all&#1111;"Radio"+i];
 	 	if (el.checked==true) flag=true;
 	 &#125;
 	if (flag==false) &#123;alert("Please enter Radio");return false;&#125;
 	else Form1.submit();
 &#125;
</script>
<form name=Form1 ACTION="http://www.some.com" METHOD=POST>
 <input type="radio" ID="Radio1" VALUE="Radio1" onclick="SelectRad(this)"> Radio 1
 <input type="radio" ID="Radio2" VALUE="Radio2" onclick="SelectRad(this)"> Radio 2
 <input type="radio" ID="Radio3" VALUE="Radio3" onclick="SelectRad(this)"> Radio 3
 <input type="radio" ID="Radio4" VALUE="Radio4" onclick="SelectRad(this)"> Radio 4
 <input type="button" VALUE="Click" onclick="ValidateRadios()">
</form>
This works in IE6. If you are using IE5 and this is still not working then update to 6 since there are some javascript issues in IE5

Posted: Tue Jun 15, 2004 3:30 pm
by Kingo
Thanx very much , I really appreciate your help

Posted: Wed Jun 16, 2004 7:45 am
by Kingo
Easiest way to do is.

Code: Select all

if(!(Form1.Radio1.checked || Form1.Radio2.checked || Form1.Radio3.checked || Form1.Radio4.checked))			
				&#123;
					alert("Please Select an Radio Button.");
					return false;
				&#125;
Where Form1 is the name of the form

Posted: Wed Jun 16, 2004 7:48 am
by patrikG
The Forms Tutorial in the Wiki would be the right resource to tap into: http://wiki.devnetwork.net/index.php/FormsTutorial