Radio Button Validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Radio Button Validation

Post 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]
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

No need for validation

Post 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.
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

THe user should get a popo up box asking to select the radio button if in case he forgets....
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Set a default value :

Code: Select all

<input type="radio" ID="Radio1" VALUE="Radio1" NAME="RadioGroup" CHECKED> Radio 1
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I donot want to set a default value.SO, trying to validate if the radio button is checked
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I'm trying to do this with Java Script
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

like i said thats your best bet but best of luck anyways
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Thanx very much , I really appreciate your help
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

The Forms Tutorial in the Wiki would be the right resource to tap into: http://wiki.devnetwork.net/index.php/FormsTutorial
Post Reply