JavaScript and client side scripting.
Moderator: General Moderators
Kingo
Forum Contributor
Posts: 146 Joined: Thu Jun 03, 2004 9:38 am
Post
by Kingo » Tue Jun 15, 2004 2:03 pm
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
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Jun 15, 2004 2:30 pm
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 » Tue Jun 15, 2004 2:43 pm
THe user should get a popo up box asking to select the radio button if in case he forgets....
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Jun 15, 2004 2:52 pm
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 » Tue Jun 15, 2004 2:54 pm
I donot want to set a default value.SO, trying to validate if the radio button is checked
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 » Tue Jun 15, 2004 3:00 pm
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 » Tue Jun 15, 2004 3:07 pm
I'm trying to do this with Java Script
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 » Tue Jun 15, 2004 3:11 pm
like i said thats your best bet but best of luck anyways
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Jun 15, 2004 3:21 pm
Code: Select all
<script language="javascript">
function ValidateRadios()
{
flag=false;
for (var i=1;i<=4;i++)
{
var el=document.allї"Radio"+i];
if (el.checked==true) flag=true;
}
if (flag==false) {alert("Please enter Radio");return false;}
else Form1.submit();
}
</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 » Tue Jun 15, 2004 3:30 pm
Thanx very much , I really appreciate your help
Kingo
Forum Contributor
Posts: 146 Joined: Thu Jun 03, 2004 9:38 am
Post
by Kingo » Wed Jun 16, 2004 7:45 am
Easiest way to do is.
Code: Select all
if(!(Form1.Radio1.checked || Form1.Radio2.checked || Form1.Radio3.checked || Form1.Radio4.checked))
{
alert("Please Select an Radio Button.");
return false;
}
Where Form1 is the name of the form