...

View Full Version : validating radio buttons



alleyhector
10-28-2004, 04:51 AM
i know this is simple, but for some reason its just stumping me and i know that after awhile i can't look at my own code. i won't be able to see the simplest mistakes. anyway, here is what i have right now. can anyone tell me why its not working?
thanks so much.


<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">
function ValidatePizza() {

for (var i=0; i < document.frmPizza.hradPizzaSize.length; i++){
if (document.frmPizza.hradPizzaSize[i].checked){
return true
}
}else {
alert ("Please choose a size for your pizza");

return false
}
}
</script>

</head>

<body>
<form id="frmPizza" name="frmPizza" onsubmit="return ValidatePizza();" action="javatest2.htm" method="post" runat="server">


<P><STRONG>Pick a size for your pizza:</STRONG></P>
<P><INPUT type="radio" value="Small" name="hradPizzaSize" runat="server">Small($9)
<INPUT type="radio" value="Medium" name="hradPizzaSize" runat="server">Medium($12)
<INPUT type="radio" value="Large" name="hradPizzaSize" runat="server">Large($15)
<INPUT type="radio" value="Giant" name="hradPizzaSize" runat="server">Giant($17)</P>
<P><INPUT id="subAddToOrder" style="TEXT-ALIGN: center" type="submit" value="Add to Order" name="subAddToOrder"></P>

</form>

hemebond
10-28-2004, 06:10 AM
Just set one radio as the default, then you don't need to validate them. Plus, this has been explained several times already on these forums.

alleyhector
10-28-2004, 07:15 AM
i apologize if this is repetitive. i know what code i should be using in theory. and i have tried many versions read many examples. this is why i need to ask a seemingly repetitive question again. it's like editing an engligh paper, you know all your grammer you just can't see it after re-reading your paper again and again.

the setting initially is a good idea but i cannot use that in this case.

any help is still MUCH appreciated.

adios
10-28-2004, 07:26 AM
This is one of those places wher a little pseudocode (http://www.webopedia.com/TERM/P/pseudocode.html) would probably help. Take a look at your logic: loop through the radios (good), return true if a checked one is found (good), and - oops - if it isn't checked, execute that else statement and prompt. What you intended, naturally, was for the loop to run its course and, if not returned from, perform the default action. Lose that }else {. Strictly speaking, not very admirable program flow, but it brings home the pizza...

alleyhector
10-28-2004, 08:00 AM
if i understand correctly you're saying i should jsut take out the else statement but when i do so it pops up the error message every time, even when i do check a radio button adn therefore never lets me go on to the next page...

adios
10-28-2004, 11:04 AM
function ValidatePizza()
{
var i = 0,
r = document.frmPizza.hradPizzaSize,
rl = r.length;
for (i; i < rl; ++i)
if (r[i].checked)
return true;
alert ("Please choose a size for your pizza !");
r[0].focus();
return false;
}

Kor
10-28-2004, 12:25 PM
Another variant is to use an extra variable and to let the loop end.

function ValidatePizza() {
for (var i=0; var j=0;i < document.frmPizza.hradPizzaSize.length; i++){
if (document.frmPizza.hradPizzaSize[i].checked){j++}
}
if(j==0){alert ("Please choose a size for your pizza");return false}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum