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
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Terms Checkbox Validation

    I don't use Javascript much, but would like to learn it. I have a website that I want to validate a form, check for missing required fields and check to make sure the user checks a checkbox agreeing to the Terms and Conditions when registering.

    I found a useful script that works well when checking for missing fields, but does not work on the checkbox agreeing to the terms.

    I know very little about Javascript so any help showing me how to make sure the checkbox for the terms is checked before submitting the form would be very much appreciated.

    Here is the script I found online that works well other than on the checkbox

    Code:
    <script language="JavaScript">
    <!--
    
    /***********************************************
    * Required field(s) validation script
    ***********************************************/
    
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("fname", "lname", "email", "phone", "terms");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("First Name", "Last Name", "Email Address", "Phone Numbers", "Terms and Conditions");
    	// dialog message
    	var alertMsg = "Please complete the following fields:\n";
    	
    	var l_Msg = alertMsg.length;
    	
    	for (var i = 0; i < fieldRequired.length; i++){
    		var obj = formobj.elements[fieldRequired[i]];
    		if (obj){
    			switch(obj.type){
    			case "select-one":
    				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "select-multiple":
    				if (obj.selectedIndex == -1){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "text":
    			case "textarea":
    				if (obj.value == "" || obj.value == null){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			default:
    			}
    			if (obj.type == undefined){
    				var blnchecked = false;
    				for (var j = 0; j < obj.length; j++){
    					if (obj[j].checked){
    						blnchecked = true;
    					}
    				}
    				if (!blnchecked){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    			}
    		}
    	}
    
    	if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    }
    // -->
    </script>
    
    <form id="form1" name="form1" method="post" action="process.php" onsubmit="return formCheck(this);">
    Thank you in advance!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    If there is only one checkbox with that name:

    Code:
    if(document.forms["formname"].checkBoxName.checked) { //code for true 
    }
    else { //code for false 
    }
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    airpirate (07-23-2012)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. I used your suggestion and re-wrote a script that works

    Code:
    <script language="JavaScript">
    <!--
    function validateForm()
    {
    var x=document.forms["form1"]["fname"].value;
    if (x==null || x=="")
      {
      alert("First Name must be filled out");
      return false;
      }
      
      var x=document.forms["form1"]["lname"].value;
    if (x==null || x=="")
      {
      alert("Last Name must be filled out");
      return false;
      }
      
      var x=document.forms["form1"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      {
      alert("Please enter valid e-mail address");
      return false;
      }
      
      var x=document.forms["form1"]["phone"].value;
    if (x==null || x=="")
      {
      alert("A valid Phone Number is required");
      return false;
      }
      
      if(document.forms["form1"].terms.checked) { return true; 
    }
    else { alert("Please Read and Acknowledge that you Agree to the Terms and Conditions");
      return false; 
    }
      
    }
    
    
    // -->
    </script>

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And if you don't KNOW if there is one or many ahead of time, test for it:
    Code:
    		var obj = formobj.elements[fieldRequired[i]];
    		if (obj){ 
                            if ( obj.length != null ) 
                            {
                                // must be multiple checkboxes, radio buttons, or <select multiple>
                                for ( var n = 0; n < obj.length; ++n )
                                {
                                    var subobj = obj[n];
                                    switch( subobj.type )
                                    {
                                          ... etc. ...
                                    }
                                }
                            } else {
                                // must be a single element
                                switch(obj.type){
                                    case "checkbox": 
                                        if ( obj.checke ) { ... } else { ... } 
                                        break;
                                    case ...
    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.


  •  

    Posting Permissions

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