problem with radio validation, need help urgent, pls

Discussion in 'JavaScript' started by nvidia, Dec 27, 2006.

  1. #1
    Hi, i am trying to validate my radio buttons using my for loop in my javascript, but it does not go into my for loop.
    
      <script language="JavaScript">
    <!--
    function validate_radio(myForm)
    {
    alert("inside function");
    
    var radioGroup = myForm.myRadios;
    var radioGroupSelected = false;
    var radIdx;
    
       for(radIdx = 0; radIdx < radioGroup && !radioGroupSelect; radIdx++)
      {
         if (radioGroup[radIdx].checked)
          {
                   radioGroupSelected = true;
          }
          else if (!radioGroupSelected)
          {
                 alert("Please select an option");
          }
         return radioGroupSelected;
      }
      alert("outside of loop")
    }
    -->
    </script>
     </head>
    
    Code (markup):
    
     <body>
      <center>
       <form>
               Hugh Jackman <input type="radio" name="myRadios" value="Hugh Jackman "> <br />
    
              Tom Cruise <input type="radio" name="myRadios" value="Tom Cruise " ><br />
    
              Halle Berry <input type="radio" name="myRadios" value="Halle Berry " ><br />
    
          <input type="submit"   onclick="return validate_radio(this.form)" />
      </form>
      </center>
     </body>
    </html>
    
    Code (markup):
    Can anybody suggest any reason why? Any help would be greatful.:confused:
     
    nvidia, Dec 27, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Untested:

    
    
      <script language="JavaScript">
    <!--
    function validate_radio(myForm)
    {
    	var radioGroup = myForm.myRadios;
    		
    	for (var i = 0; i < radioGroup.length; i++)
    	{
    		if (radioGroup[i].checked)
    		{
    			return true;
    		}
    	}
    	
    	alert('Please select at least one.');
    	return false;
    }
    
    -->
    </script>
    
    Code (javascript):
    And add this to your form:
    
    
    <form onsubmit="return validate_radio(this);" method="post" action="">
    
    Code (markup):

    EDIT:


    I just see you're not submitting the form. Take out the "form" in the onclick attribute.

    
    onclick="return validate_radio(this)"
    
    Code (markup):
     
    nico_swd, Dec 27, 2006 IP
  3. nvidia

    nvidia Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you, problem solved.:D
     
    nvidia, Dec 27, 2006 IP