Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validating radio buttons

    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>

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    This is one of those places wher a little pseudocode 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...

  • #5
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    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;
    }
    Last edited by adios; 10-28-2004 at 04:27 PM.

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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}
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •