By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

validation of radio button

P: 8
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
Share this Question
Share on Google+
5 Replies


100+
P: 136
hi

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

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

susen
Mar 14 '07 #2

P: 8
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

P: 8
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

P: 8
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

Expert 100+
P: 1,892
Just curious your form only has 1 radio button? Or is that only part of the form?
Mar 15 '07 #6

Post your reply

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