javascript form validation for a checkbox not working

Discussion in 'Software' started by izzysanime, Apr 19, 2007.

  1. izzysanime

    izzysanime Private E-2

    Hi,

    I have a form validation script in JS, from dynamicdrive.com it is supposed to require a checkbox, it works in their demo, but not mine. Any quick ideas? I have set the value of the check box to 1 or nothing. Nothing works. All the other form fields that are required work.
    The temp address is http://wareriver.com/clients/fs/form.html

    thanks
    Josh
     
  2. Mada_Milty

    Mada_Milty MajorGeek

    This is the tag for your checkbox:

    Code:
    <input name="t27" type="checkbox" id="t27" value="1" />
    And this is the validation function:

    Code:
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("customerInfoLastName", "customerInfoFirstName", "customerInfoAddress", "customerInfoCity", "customerInfoState", "customerInfoZip", "customerInfoPhones", "vehicleVIN1", "vehicleYear1", "vehicleMake1", "vehicleModel1", "pickupInfoLastName", "pickupInfoFirstName", "pickupInfoAddress", "pickupInfoCity", "pickupInfoState", "pickupInfoZip", "pickupInfoPhones", "deliveryInfoLastName", "deliveryInfoFirstName", "deliveryInfoAddress", "deliveryInfoCity", "deliveryInfoState", "deliveryInfoZip", "deliveryInfoPhones", "t27");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("Customer Info - Last Name",  "Customer Info - First Name", "Customer Info - Address", "Customer Info - City", "Customer Info - State", "Customer Info - Zip", "Customer Info - Phone #", "vehicle VIN #", "vehicle Year", "vehicle Make", "vehicle Model", "Pickup Info - Last Name", "Pickup Info - First Name", "Pickup Info - Address", "Pickup Info - City", "Pickup Info - State", "Pickup Info - Zip", "Pickup Info - Phone Number", "Delivery Info - Last Name", "Delivery Info - First Name", "Delivery Info - Address", "Delivery Info - City", "Delivery Info - State", "Delivery Info - Zip", "Delivery Info - Phone Number", "You Must Agree to the Terms & 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;
    	}
    }
    Now, we have the id of the checkbox in your "fieldRequired" array, so we're good there.

    However, in your switch, where we're checking the types of the required fields, there is no case for "checkbox", so it is going to the default. The default case only checks if the object type is undefined, which it is NOT. So nothing gets executed! (Also, it looks like there's an extra } there!)

    We'll need to add a case for checkboxes. Try adding this:

    Code:
    case "checkbox":
         if (obj.checked == false) {
              alertmsg += " - " + fieldDescription[i] + "\n";
         }
         break;
     
  3. izzysanime

    izzysanime Private E-2

    Thanks for your help. I appreciate it ^_^ I tried that, but for whatever reason the validation does not even happen after I add that code. I got the script off of dynamicdrive.com because it worked with checkboxes already. but no one in that forum will help.
    Should I upload the changed form with your code to show you what I mean, would that help?

    Thanks
    Josh
     
  4. Mada_Milty

    Mada_Milty MajorGeek

    I could be mistaken about the types we're checking for, it just seems unusual you can define a type in the INPUT tag, but it isn't returned???

    I'd still say that the brackets are a problem.

    Notice your default: case.... why is there a close-brace ( } ) directly underneath?? Have you corrected this?

    That bracket is not needed there, but you will need an extra close-brace to close out the for loop.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds