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 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Smile Radio Button Validation

    Wondering how come this doesn't work any ideas? its validating that the radio buttons have been selected.

    Code:
    if ( ( form.q1[0].checked == false ) && ( form.q2[1].checked == false) && ( form.q3[2].checked == false )) { alert ( "Please Choose a " ); return false; }

  • #2
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    Um surely you should be using check-boxes here.

    A group of radios should always have at least one member checked, they are an either or mechanism.

    You can validate against the selected value of a group of radios, what you want to be doing is validating if check-boxes are checked.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    To validate radios:-

    Code:
    <form name= "myform">
    <input type = "radio" name = "rad1" value = "NE">North East
    <input type = "radio" name = "rad1" value = "NW">North West
    <input type = "radio" name = "rad1" value = "SE">South East
    <input type = "radio" name = "rad1" value = "SW">South West
    <input type = "radio" name = "rad1" value = "Midlands">Midlands
    <br><br>
    <input type = "button" value = "Which Radio Selected?" onclick = "chkrads()">
    </form>
    
    <script type = "text/javascript">
    
    function chkrads() {
    var chosen = "None";
    var which = -1;
    var len = document.myform.rad1.length;
    for (i = 0; i <len; i++) {
    if (document.myform.rad1[i].checked) {
    chosen = document.myform.rad1[i].value;
    which = i;
    }
    }
    if (chosen == "None") {
    alert("No Location Chosen");
    }
    else {
    alert("You selected  " +  chosen +  " which is Radio Button " + which );
    }
    }
    
    </script>
    To validate checkboxes:-

    Code:
    <form name = "myform">
    <input name = "myname1" type = "checkbox" value = "Red"> RED <br>
    <input name = "myname2" type = "checkbox" value = "Blue"> BLUE <br>
    <input name = "myname3" type = "checkbox" value = "Green"> GREEN <br>
    <input name = "myname4" type = "checkbox" value = "Yellow"> YELLOW<br><br>
    <input type = "button" value = "Click Me" onclick="cboxlist()"><br>
    </form>
    
    <script type = "text/javascript">
    
    function cboxlist(){
    var choice = "Your colour choice is: ";
    var x = "";
    for (var i=1; i<5; i++) {
    x = document.myform['myname' + i].value;
    if (document.myform['myname' + i].checked) {
    choice = choice + "  " +  x ;    
    } 
    }  
    if (choice.length <= 24) {
    choice = choice + "NONE";
    }
    alert (choice);
    } 
    
    </script>

    Quizmaster: The Mississippi flows through almost the entire length of which country?
    Contestant: Egypt.

  • Users who have thanked Philip M for this post:

    bubble00000 (06-16-2010)

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for that, finally got it working (y) ..

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Quote Originally Posted by mattyod View Post
    A group of radios should always have at least one member checked, they are an either or mechanism.
    *NOT TRUE*. Unless you specify the CHECKED property for one of the radio buttons in your HTML, then it is quite possible that *NONE* of the buttons will be checked by the user.

    A group of radio buttons will have either ZERO or ONE button checked.
    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.

  • #6
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Old Pedant View Post
    *NOT TRUE*. Unless you specify the CHECKED property for one of the radio buttons in your HTML, then it is quite possible that *NONE* of the buttons will be checked by the user.

    A group of radio buttons will have either ZERO or ONE button checked.
    Note the use of the word "should" in my original post


  •  

    Posting Permissions

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