Web Analytics Made Easy -
StatCounter Radio Validation not working - CodingForum

Announcement

Collapse
No announcement yet.

Radio Validation not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Radio Validation not working

    I wrote a function to to validate some elements in my form and for some reason I can't seem to figure out why my radio validation is not working. There are about 3 to 4 radio buttons. all with the same name, but i need to make sure that at least one out of this group is selected.

    Below is my code. Any help would be very useful. Thanks!

    JS:
    Code:
    <script type="text/javascript">	
    	
    function validate_required(field,alerttxt,select)
    {
    with (field)
      {
      if (value==null||value=="")
        {
        alert(alerttxt);return false;
        }
      else
        {
        return true;
        }
      }
      
    }
    
    function validate_form(thisform)
    {
     with (thisform)
      {
      if (document.getElementsByName('sleeper').checked==false)
      {
      window.alert('Please select a sleeper')
       return false;}
      }
      } //last
    </script>
    Form:
    Code:
    <form name="week_picks" method="post" action="confirm.php" onsubmit="return validate_form(this)">
    
    
    <input name="sleeper" type="radio"  value="<?=$sleeper;?>" />
    
    <input name="sleeper" type="radio"  value="<?=$sleeper;?>" />
    
    <input name="sleeper" type="radio"  value="<?=$sleeper;?>" />
    
    etc...
    </form>
    Generic signature comment!

  • #2
    you need to first get the collection
    (many examples in this forum)
    then loop through and check
    each element
    Last edited by DaveyErwin; Sep 6, 2011, 05:29 PM.

    Comment


    • #3
      And why would you use document.getElementsByName('sleeper') instead of simply
      Code:
      function validate_form(thisform)
      {
          if ( getRadioValue(thisform.sleeper) == null ) 
          {
              alert('Please select a sleeper')
              return false;
          }
          ... other validation ...
      }
      
      function getRadioValue(rbgroup)
      {
          if ( rbgroup.length == null ) return rbgroup.checked ? rbgroup.value : null;
          for ( var r = 0; r < rbgroup.length; ++r )
          {
              if ( rbgroup[r].checked ) return rbgroup[r].value;
          }
          return null;
      }
      ...
      N.B.: That first line in the getRadioValue function is only needed just in case you might happen to have some radio button "group" that only has one radio button in it. If that happens, you won't really have a group (that is, a collection), so you have to check the single button element.
      Be yourself. No one else is as qualified.

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎