473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

validation of radio button

hi im validating radio buttons

i create dis radio button in php based on some how many records of my query.
i wrote a javascript to validate this buttons.
wat i do is dat wen no radio button are selected it should giv message dat select a radio button.
else it should go to other page.


My code works fine when there are more than 1 radio button
but when only 1 radio button is der dat time it does not work

the thing is dat i calculate the length of radio button in javascript.
and based on dat i do my validation

but wen 1 radio is der my program take the length as 'undefied'
and becoz of this it does not work

plz help me to sort it out.


im giving my code below

function validate(thisform)
{
var arrayLength = thisform.length;
var checkthis;
var radio_selected;
if (!arrayLength)
{
if (thisform.checked != 1)
{
alert("Please select.");
return false;
}
else
{
document.form1.method="post"
document.form1.action=".php"
document.form1.submit()
}
}
else
{
for (i=0; i < arrayLength; i++)
{
checkthis = thisform[i];
if (checkthis.checked == 1)
{
radio_selected = "yes";
break;
}
}
if (radio_selected != "yes")
{
alert("Please select.");
return false;
}
else
{
document.form1.method="post"
document.form1.action=".php"
document.form1.submit()
}
}
}

dis does not work for wen 1 radio button is der
Mar 14 '07 #1
5 2695
devsusen
136 100+
hi

write ----->>>> if ( ! thisform.checked)

instead of ----->>>> if (thisform.checked != 1)

susen
Mar 14 '07 #2
hi

write ----->>>> if ( ! thisform.checked)

instead of ----->>>> if (thisform.checked != 1)

susen

thanks 4 the reply!!
but still not working

wen i say submit before selecting a radio it gives me appropriate msg

i.e

if (!thisform.checked)
{
alert("Please make a selection.");
return false;
}

it goes here

but if i select a radio dan also it goes in same loop
if (!thisform.checked)
{
alert("Please make a selection.");
return false;
}



il give u my whole code plz c dis.i badly need help here.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>swati</title>
<script language="javascript">

function validate(thisform)
{
var arrayLength = thisform.length;
var checkthis;
var radio_selected;
if (!arrayLength)
{
if (!thisform.checked)
{
alert("Please make a selection.");
return false;
}
else
{
//alert("im in chk")
document.form1.method="post"
document.form1.action=".php"
document.form1.submit()
}
}
else
{
for (i=0; i < arrayLength; i++)
{
checkthis = thisform[i];
if (checkthis.checked == 1)
{
radio_selected = "yes";
break;
}
}
if (radio_selected != "yes")
{
alert("Please make a selection.");
return false;
}
else
{
document.form1.method="post"
document.form1.action=".php"
document.form1.submit()
}
}
}

</script>
</head>

<body>
<form id="form1" name="form1">
<h3>select the option</h3>
<?php
$postvar=10;
$i;
for($i=0;$i<1;$i++)
{
echo "<input type=\"radio\" name=\"check\" value='$postvar'><br>";
$postvar=$postvar-1;
}
?>
<input type="button" value="Submit" onclick="validate(this)"/>
<!--input type="submit" value="submit" onclick="go1()"/-->
</form>
</body>
</html>
Mar 15 '07 #3
im getting this prob only when there is 1 radio button


actually i was thinking that when there is 1 radio button il assume the length to be 1.n than chk
but i cant do that also coz wen no radio buttons are checked dat time also length is undefined
so no point in using dat logic
Mar 15 '07 #4
function validate1()
{
var arrayLength = document.form1.check.length;
var checkthis;
var radio_selected;
//alert(arrayLength)
if (!arrayLength)
{
if (!document.form1.check.checked)
{
alert("Please make a selection.");
return false;
}
else
{
document.form1.method="post"
document.form1.action=".php"
document.form1.submit()
}
}
else
{
for (i=0; i < arrayLength; i++)
{
checkthis = document.form1.check[i];
if (checkthis.checked == 1)
{
radio_selected = "yes";
break;
}
}
if (radio_selected != "yes")
{
alert("Please make a selection.");
return false;
}
else
{
document.form1.method="post"
document.form1.action=".php"
document.form1.submit()
}
}
}
i tryd this code now its working fine.
anyways thanks for ur help
Mar 15 '07 #5
AricC
1,892 Expert 1GB
Just curious your form only has 1 radio button? Or is that only part of the form?
Mar 15 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shane | last post by:
I've got a C# ASP.NET web form that fails to validate under certain conditions. I have a radio button that causes an AutoPostback that enables certain other elements on the form. If I click that...
2
by: Shane | last post by:
I've got a C# ASP.NET web form that fails to validate under certain conditions. I have a radio button that causes an AutoPostback that enables certain other elements on the form. If I click that...
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
5
by: Mattyw | last post by:
Hi, I'm relatively new to Web Forms, I have been using Required Field Validators and Regular Expression Validators on a Web Form I am developing and everything works as expected using Visual...
0
by: Gary W. Smith | last post by:
Hello, I have a simple form with a radio button group on it and I need to do some basic validation based upon which button is checked. Here is a little more detail. if button one is...
8
by: photoboy | last post by:
I have racked by brain long enough on this, so now I need the help of someone who knows what they are doing. Here is what I am trying to achieve: First, I have two radio buttons (both unchecked)...
4
by: pureadrenaline | last post by:
Hey Guys, Please could anyone help me out with the following form I need to create a validation on the email field only if the user checked the radio button named Email. Thanks in advance. ...
2
by: poreko | last post by:
I am trying to validate the inputs of a form using javascript. The validation is working but the form is not been submitted after the inputs have been checked. I have been unable to find any error....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.