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 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    genvalidator and checkboxes

    i'm using
    http://www.javascript-coder.com/html...lidation.phtml

    now I want to know how to validate a checkbox

    if i have

    <input type=checkbox id="c2" name=33>dd&nbsp;<input type=checkbox id="c2" name=33>ff.aa&nbsp;<input type=checkbox id="c2" name=33>ddd&nbsp;

    is it correct to have them all with the same id and how do I validate it using this validation routine?
    (or if this one doesn't work is there a specific validation functions -your can recommend that are easy to use for all different types of input)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    No, you can't use the same ID for more than one object.

    What are you trying to validate? That at least one of them is checked? Or what?

    Not really a good idea to start the name of an object with a digit. Better would be something like name="cb33" and the name should be enclosed in quotes.

    But in any case, you don't say WHAT you want to validate for.

    Just guessing, but you might be able to use selone. It was intended for radio buttons, but if you have a group of same-named checkboxes they are nearly indistinguishable from a group of radio buttons.
    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.

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts
    yes I want to make sure at least one is checked

    if i use selone

    or selmin=1

    what id do I use if each option has a different id?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Code:
    <form name="form1">
    
    <input type="checkbox" name="checked[]" value="1">
    <input type="checkbox" name="checked[]" value="2">
    <input type="checkbox" name="checked[]" value="3">
    <input type="checkbox" name="checked[]" value="4">
    <input type="checkbox" name="checked[]" value="5">
    <input type="checkbox" name="checked[]" value="6">
    
    <input type="button" value="Check" onclick="chkChecks('checked[]')">
    
    </form>
    
    <script type="text/javascript">
    
    var checkedVals = "You checked boxes:  ";
    function chkChecks(nme) {
    var isChecked = 0;
    var f = document.forms.form1;
    var c = document.getElementsByName(nme);
    for (var i = 0; i < c.length; i++) {
    if (c[i].checked) {
    isChecked ++;
    
    //alert (document.form1.elements['checked[]'][i].value);  // display value of checked checkbox
    checkedVals = checkedVals + (document.form1.elements['checked[]'][i].value) + " ";
    
    }
    }
    
    if (isChecked <1) {  // at least one checkboxmust be selected - change value for 2 or more
    alert('Please select at least one checkbox');
    return false;
    }
    else {
    alert (checkedVals);
    checkedVals = "You checked boxes:  ";
    }
    }
    
    </script>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    According to the docs for that system, you do *NOT* use IDs. You use names. And it's perfectly okay for multiple objects to have the same name.

    I quote from that page you pointed to:
    The format of the addValidation() function is:

    frmvalidator.addValidation(Field Name, Validation Descriptor, Error String);
    I have no idea why they added id's to any of those fields. Almost surely the id's are completely irrelevant.

    BUT... But that's from reading the docs. No experience with using this product. To me, it seems like more work than it is worth.
    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
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    whether you name all the checkboxes with the same name or not depends on the circumstances, but the concept behind checking if any checkboxes have been checked is much the same in either case.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
            <script type="text/javascript">
                function validateForm(){
                    var oChkBoxes = document.getElementById('chkContainer').getElementsByTagName('input');
                    var isChkBoxChecked = false;
                    for(i=0; i < oChkBoxes.length; i++){
                        if(oChkBoxes[i].checked){
                          isChkBoxChecked = true;
                          i = oChkBoxes.length;
                        }
                    }
                    if(!isChkBoxChecked){alert('Please check at least 1 checkbox');}
                    return isChkBoxChecked;
                }
            </script>
        </head>
        <body>
            <form action="" method="" onsubmit="return validateForm();">
                <div id="chkContainer">
                    <input type="checkbox" name="chk1" value="1" />
                    <input type="checkbox" name="chk2" value="2" />
                    <input type="checkbox" name="chk3" value="3" />
                    <input type="checkbox" name="chk4" value="4" />
                </div>
                <input type="submit" value="Submit" />
            </form>
        </body>
    </html>

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Both Phillip and Bullant supplied quite nice answers that are quite workable on their own, but neither would fit well into the way genvalidator works. There are specific rules for adding a custom validator to it that you both ignored, so unless esthera is willing to abandon genvalidator he/she would need some work to adapt your answer to its scheme.
    Last edited by Old Pedant; 03-22-2011 at 02:06 AM.
    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.

  • #8
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Old Pedant View Post
    Both Phillip and Bullant supplied quite answers that are quite workable on their own, .
    that's because that is all I intended to do

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    If you look here:
    http://www.javascript-coder.com/html...n.phtml#custom
    you can see it's reasonably easy to add a custom validator.

    But it's also kind of brain-dead. Note what it says there:
    The custom validation function will be called automatically after other validations.
    Yuck.

    If the fields your custom validator needs to check are at the start of the <form>, you'd like to have the message(s) about their validation errors appear near the start of the messages. Won't happen with this mildly brain-dead system.

    It's just one reason I said that it's more work that it's worth.

    **********

    On top of that, any custom validator you write can only apply to the ONE FORM for that specific page. You can't add it to the suite of validators for use in multiple pages. Yuck again.
    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.

  • #10
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts
    name of the field didn't work

    and id works for the others (text box and select box)

    is there a different validation that I should use? or just write my own?
    what do you recommend?


  •  

    Posting Permissions

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