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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Validating Checkboxes

    I have the following JavaScript, I need to edit it to validate and make sure that 1 of 2 checkboxes is selected... The following only checks one...

    Code:
    // Make sure a single checkbox is checked
    var boxCheck = false;
    if (document.formname.male.checked) {
    boxCheck = true; }
    if (!boxCheck) {
    theMessage = theMessage + "\n --> Agree to the terms";
    }

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,084
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Is only one checkbox required to be checked or can both be checked at the same time?
    Is it necessary that one of the two (or more) be checked?

    If only one necessary in a group, might be better to use a radio button, ie:
    o Male o Female o Unknown

    If at least one checkbox required then consider (pseudo-code):
    ...
    var maleflag = false;
    var femaleflag = false;
    var unknownflag = false;
    if (male.checked) { maleflag = true; }
    if (female.checked) { femaleflag = true; }
    if (unknown.checked { unknownflag = true; }
    var grpflag = maleflag || femaleflag || unknownflag;
    if (grpflag == true) { alert('do TRUE stuff'); } else { alert('do FALSE stuff'); }
    ...

    Alternatively, you could count like this (again pseudo-code):
    ...
    var grpflag = 0;
    if (male.checked) { grpflag += 1; }
    if (female.checked) { grpflag += 2; }
    if (unknown.checked) { grpflag += 4; }
    if (grpflag < 2) { alert('male') }
    else { if grpflag < 3) { alert('female') } else { alert('HUH?'); }
    }
    ...

    grpflag would represent
    1 = male
    2 = female
    3 = what? (male and female)
    4 = unknown (might want to check and come back later)
    5 = what? (male and unknown)
    6 = what? (female and unknown)
    7 = what? (male and female and unknown)

    Good luck!

  • #3
    Regular Coder PremiumBlend's Avatar
    Join Date
    Apr 2006
    Location
    Marion, Iowa
    Posts
    201
    Thanks
    0
    Thanked 13 Times in 13 Posts
    If this is to check whether the user will "Agree to the terms", you should probably use radio buttons.
    My Website: DumpsterDoggy

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi guys... I am working with a JavaScript already and not sure how to add to it from the above...

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin

    function validate() {

    var theMessage = "Please complete the following: \n-----------------------------------\n";
    var noErrors = theMessage

    // make sure field is not blank
    if (document.form.workphone.value=="") {
    theMessage = theMessage + "\n --> Work telephone";
    }

    // If no errors, submit the form
    if (theMessage == noErrors) {
    return true;

    } else {

    // If errors were found, show alert message
    alert(theMessage);
    return false;
    }
    }
    // End -->
    </script>
    Basically I need to add a check to make sure that 1 of 5 radio buttons have been pressed, also that 1 of 2 radio buttons been checked and also, that atleast 1 of 3 checkboxes have been checked... Any ideas? I'm kind of stumped...


  •  

    Posting Permissions

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