SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validate Multiple Check Boxes?

    I'm not really the best with Javascript and it took me a bit just to find the code to validate one check box.

    Code:
    <script type="text/javascript"> 
    function validate(form) { 
    
    if(document.form1.checkbox1.checked == false)
    	{alert("Application Terminated" + "\n---------------------------------" + "\nThe response(s) provided do not match the required response for the position. Thank you for your interest and your time."); 
    	return false; } 
    
    	return true;
    	}
    
    </script>
    Is there a way I can modify this to include another checkbox in the validation:
    Code:
    document.form1.checkbox2.checked == false
    Any help would be greatly appreciated

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function validate(form) { 
        var invalid = false;
        if(form.elements['checkbox1'].checked == false) {
            invalid = true;
        }
        if(form.elements['checkbox2'].checked == false) {
            invalid = true;
        }
        if (invalid) {
            alert("Application Terminated" + "\n---------------------------------" + "\nThe response(s) provided do not match the required response for the position. Thank you for your interest and your time."); 
            return false;
        }
        return true;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or
    Code:
    function checkRadio (frmName, rbGroupName){
      var radios = document[frmName].elements[rbGroupName];
       for (var i=0; i<radios.length;i++)
        {
    	 if (radios[i].checked) {
    	  return true;
    	  }
    	}
    	return false; 
     }
    ========================
    Carn the Tiges!
    www.petalsandpatches.com

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    function validate(form) {
        var invalid = !(form.elements['checkbox1'].checked && form.elements['checkbox1'].checked);
        if (invalid)
            alert("Application Terminated" + "\n---------------------------------" + "\nThe response(s) provided do not match the required response for the position. Thank you for your interest and your time.");
        return !invalid;
    }

    I love boolean operators.
    Last edited by M-M-J; Jun 23, 2008 at 04:05.
    mmj

  5. #5
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks so much! this has made my day!


Bookmarks

Posting Permissions

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