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 3 of 3
  1. #1
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 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>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    you need to first get the collection
    (many examples in this forum)
    then loop through and check
    each element
    Last edited by DaveyErwin; 09-06-2011 at 10:29 PM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,422 Times in 4,387 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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