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,179
    Thanks
    174
    Thanked 257 Times in 257 Posts

    Easiest way to go about validating a multi-named radios

    I have a large form for a user to fill out. There are probably about 50 different named sections of radio buttons. What would be the easiest way of validating them.

    EX of what i have (that doesnt work)

    Code:
    function checkRadio (overview) {
     var radios = document[overview];
     for (var i=0; i <radios.length; i++) {
      if (radios[i].checked) {
       return true;
      }
     }
     return false;
     alert("bad");
    }
    html
    Code:
    <form id="overview" name="overview" action="" onsubmit="checkRadio(this)">
    
    <input name="lawn" type="radio" value="g" />
    <input name="lawn" type="radio" value="g" />
    <input name="lawn" type="radio" value="g" />
    
    <input name="house" type="radio" value="g" />
    <input name="house" type="radio" value="g" />
    <input name="house" type="radio" value="g" />
    and about 48 more of those. Anyone have an easy solution to this, or any help.

    Thanks

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by jcdevelopment View Post
    I have a large form for a user to fill out. There are probably about 50 different named sections of radio buttons. What would be the easiest way of validating them.
    In the specified form, this function should alert the names of any unchecked radio or checkboxes (groups or singles)

    Usage: <form ........ onsubmit="return allChecked(this)">

    Code:
    <script type='text/javascript'>
    
    function allChecked(theForm) 
    {
     var setNames=[], errorNames=[], elems=theForm.elements, len=elems.length, group, ok=false;
    
     for(var i=0; i<len; i++)
      if(elems[i].type && elems[i].type.match(/radio|checkbox/) && !setNames[ elems[i].name ] )
      {
       setNames[ elems[i].name ] = true;
       
       group=elems[elems[i].name];
       
       ok=false;
       
       if(group.length)
        for(var j=0, bLen = group.length; j < bLen && !(ok=group[j].checked); j++)
        ;
       else
        ok=group.checked;
        
       if(!ok)
        errorNames.push(elems[i].name);
      }
      
     if( !(ok=(errorNames.length==0)) )
      alert('Please check one in each of these groups:\n\n'+errorNames);
      
     return ok; 
    } 
    
    </script>

  • Users who have thanked Arty Effem for this post:

    jcdevelopment (09-10-2008)

  • #3
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,179
    Thanks
    174
    Thanked 257 Times in 257 Posts
    Thanks, it worked great. I understand a good deal of why its working and what its doing.. but for some reason it is still submitting the form. Do you know anyway to stop that. I tried returning it false but it still went.

    Thanks

    EDIT** Nevermind, i missed the return on the submit

    Code:
    onsubmit="return function"
    Last edited by jcdevelopment; 09-10-2008 at 03:19 PM.


  •  

    Posting Permissions

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