SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Checkbox validation doing my head in!

    Hey guys,

    I have a simple form which I'm trying to validate. I have around 8 checkboxes that the user has to agree with, and if they dont, they get the message saying "you did not accept the training princples, yada yada".

    I also have a few text fields asking for their details.

    Maybe I'm missing something simple here, but everytime I submit the form, it doesnt validate. Im not fantastic at coding, but I was hoping this would be quite straight forward. All I'm trying to do is get a message up if the user doesnt agree with all of the checkboxes.

    An example for one of the checkboxes below:

    Code:
    <p><input type="checkbox" name="first" id="first_chk">&nbsp;<label for="first_chk">Free sharing of materials between all forum members <span class="red">*</span></label></p>
    Here is the function that handles the validation:

    Code:
    function checkForm(form){
    	var ret=true;
    	var reason="";
    	
    	 if      (gE("first_chk").checked == false &&
                    (gE("second_chk").checked == false &&
    		(gE("third_chk").checked == false &&
    		(gE("fourth_chk").checked == false &&
    		(gE("fifth_chk").checked == false &&
    		(gE("sixth_chk").checked == false &&
    		(gE("seventh_chk").checked == false &&
    		(gE("eight_chk").checked == false &&
    	 	(gE("ninth_chk").checked == false) {
    	  
    	 reason+="* You did not accept the following principles \n";
    	 ret=false;
         }
    	
    	if (form.firstname.value=="") {
    	    reason+="* You did not enter your first name\n";
    	    ret=false;
    	}
    		
    	if (form.surname.value=="") {
    	    reason+="* You did not enter your surname\n";
    	    ret=false;
    	}
    	
    	if (form.jobtitle.value=="") {
    	    reason+="* You did not enter a job title\n";
    	    ret=false;
    	}
    	
    	if (form.organisation.value=="") {
    	    reason+="* You did not enter an organisation\n";
    	    ret=false;
    	}
    		
    	if (form.email.value=="") {
    	    reason+="* You did not enter a email address\n";
    	    ret=false;
    	}
    	
    	if (form.postal.value=="") {
    	    reason+="* You did not enter a postal address\n";
    	    ret=false;
    	}
    	
    	if (form.forum.value=="") {
    	    reason+="* You did not enter a financial forum\n";
    	    ret=false;
    	}
    	
    	if (form.materials.value=="") {
    	    reason+="* You did not enter any plan to use the materials\n";
    	    ret=false;
    	}
    	
    	
    	if (ret==false) {
    	    alert("Sorry, your comments have not been submitted because:\n"+reason)
    	    return false;
    	} else {
    	    return true;
    	}
    
    }
    Any help much apprecited!

  2. #2
    SitePoint Addict buildakicker's Avatar
    Join Date
    Jun 2005
    Location
    NorCal
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try ELSE IF instead of && for each of the checkbox tests.
    SKILEASES.COM - FREE rental listings!
    WILDFIREBLOG.COM - Wildland Fire microblog!

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    && should be ||

    && means, ALL true to be TRUE
    || means, ONE true to be TRUE.

    In your case you want the msg to appear if even ONE is true.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your first IF has a bunch of open parentheses that are never closed.

    Code:
    	 if      (gE("first_chk").checked == false ||
                    gE("second_chk").checked == false ||
    		gE("third_chk").checked == false ||
    		gE("fourth_chk").checked == false ||
    		gE("fifth_chk").checked == false ||
    		gE("sixth_chk").checked == false ||
    		gE("seventh_chk").checked == false ||
    		gE("eight_chk").checked == false ||
    	 	gE("ninth_chk").checked == false) {

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies.

    I've tried doing this with IF ELSE statements by still no joy.

    Am I missing anything?

    Code:
    function checkForm(form){
    	var ret=true;
    	var reason="";
    	
    	if  (gE("first_chk").checked == false) 
    		{
    		reason+="*please accept the principles for sharing training materials \n";
    		}
    	else if	(gE("second_chk").checked == false)
    		{
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else if	(gE("third_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else if	(gE("fourth_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else if	(gE("fifth_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else if	(gE("sixth_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else if	(gE("seventh_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else if	(gE("eight_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		} 
    	else (gE("ninth_chk").checked == false)				 
            {
    		reason+="*please accept the principles for sharing training materials \n";
    		}

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that if's a little unwieldly, does this work for you?
    Code:
    if (!(gE("first_chk").checked && gE("second_chk").checked
    	&& gE("third_chk").checked && gE("fourth_chk").checked
    	&& gE("fifth_chk").checked && gE("sixth_chk").checked
    	&& gE("seventh_chk").checked && gE("eight_chk").checked
    	&& gE("ninth_chk").checked)) {
    	
    	reason+="*please accept the principles for sharing training materials \n";
    }

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    I think that if's a little unwieldly, does this work for you?
    Code:
    if (!(gE("first_chk").checked && gE("second_chk").checked
    	&& gE("third_chk").checked && gE("fourth_chk").checked
    	&& gE("fifth_chk").checked && gE("sixth_chk").checked
    	&& gE("seventh_chk").checked && gE("eight_chk").checked
    	&& gE("ninth_chk").checked)) {
    	
    	reason+="*please accept the principles for sharing training materials \n";
    }
    Thanks mate that worked!

    Cheers!


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
  •