I have done this before, but for some reason it doesn't want to work with my if statements...it keeps submitting the form when nothing is filled out.

It's the validation for an html form, and there are checkboxes that display certain parts of the form. If a checkbox is checked, it needs to validate the fields within that section. type1 through type6 are the checboxes.

If I take all of the code away after the type1 if statement, it works fine. If I add it back, it starts submitting the form when it's not filled out.

If you can help me out, I'd be so grateful!

<script type="text/javascript">
function validate_form() {
	 if(document.contactform.firstname.value == '') {
		alert('Please enter your First Name.');
		return false;
	 }
	 if(document.contactform.lastname.value == '') {
		alert('Please enter your Last Name.');
		return false;
	 }
	 if(document.contactform.company.value == '') {
		alert('Please enter your Company.');
		return false;
	 }
	 if(document.contactform.address.value == '') {
		alert('Please enter your address.');
		return false;
	 }
	 if(document.contactform.city.value == '') {
		alert('Please enter your city.');
		return false;
	 }
	 if(document.contactform.state.value == '') {
		alert('Please enter your state.');
		return false;
	 }
	 if(document.contactform.zipcode.value == '') {
		alert('Please enter your zipcode.');
		return false;
	 }
	 if(document.contactform.country.value == '') {
		alert('Please enter your country.');
		return false;
	 }
	 if(document.contactform.workphone.value == '') {
		alert('Please enter your work phone.');
		return false;
	 }
	 if(document.contactform.email.value == '') {
		alert('Please enter your email.');
		return false;
	 }
	 if(document.contactform.website.value == '') {
		alert('Please enter your website.');
		return false;
	 }
	 if(document.contactform.type1.value) {
		if((document.contactform.prefcontactcs[0].checked == false) && (document.contactform.prefcontactcs[1].checked == false)) {
			alert('Please enter your Contact Preference for Customer Support.');
			return false;
		 }
		if((document.contactform.timecontactcs[0].checked == false) && (document.contactform.timecontactcs[1].checked == false)) {
			alert('Please enter your Contact Time Preference for Customer Support.');
			return false;
		 }
		 return false;
	 }
	 if(document.contactform.type2.value) {
		 if(document.contactform.organicmaterial.value == '') {
			alert('Please enter the type of organic material.');
			return false;
		 }
		if((document.contactform.prefcontactps[0].checked == false) && (document.contactform.prefcontactps[[1].checked == false)) {
			alert('Please enter your Contact Preference for Project Information.');
			return false;
		 }
		if((document.contactform.timecontactps[0].checked == false) && (document.contactform.timecontactps[1].checked == false)) {
			alert('Please enter your Contact Time Preference for Project Information.');
			return false;
		 }
		 return false;
	 }
	 if(document.contactform.type3.value) {
		if((document.contactform.prefcontactpi[0].checked == false) && (document.contactform.prefcontactpi[1].checked == false)) {
			alert('Please enter your Contact Preference for Product Information.');
			return false;
		 }
		if((document.contactform.timecontactpi[0].checked == false) && (document.contactform.timecontactpi[1].checked == false)) {
			alert('Please enter your Contact Time Preference for Product Information.');
			return false;
		 }
		 return false;
	 }
	 if(document.contactform.type4.value) {
		 if(document.contactform.modelnum.value == '') {
			alert('Please enter the model number.');
			return false;
		 }
		 if(document.contactform.serialnum.value == '') {
			alert('Please enter the serial number.');
			return false;
		 }
		 if(document.contactform.purchasedate.value == '') {
			alert('Please enter the purchase date.');
			return false;
		 }
		 if(document.contactform.locationinstall.value == '') {
			alert('Please enter the install location.');
			return false;
		 }
		 if(document.contactform.usage.value == '') {
			alert('Please enter the machine usage.');
			return false;
		 }
		 if(document.contactform.wasterecycled.value == '') {
			alert('Please enter the type of organic waste recycled.');
			return false;
		 }
		 if((document.contactform.companytype[0].checked == false) && (document.contactform.companytype[1].checked == false) && (document.contactform.companytype[2].checked == false) && (document.contactform.companytype[3].checked == false) && (document.contactform.companytype[4].checked == false) && (document.contactform.companytype[5].checked == false) && (document.contactform.companytype[6].checked == false) && (document.contactform.companytype[7].checked == false)) {
			alert('Please enter the company type.');
			return false;
		 }
		 if(document.contactform.contactperson.value == '') {
			alert('Please enter the contactperson.');
			return false;
		 }
		if((document.contactform.prefcontactts[0].checked == false) && (document.contactform.prefcontactts[[1].checked == false)) {
			alert('Please enter your Contact Preference for Technical Support.');
			return false;
		 }
		if((document.contactform.timecontactts[0].checked == false) && (document.contactform.timecontactts[1].checked == false)) {
			alert('Please enter your Contact Time Preference for Technical Support.');
			return false;
		 }
		 return false;
	 }
	 if(document.contactform.type5.value) {
		 if(document.contactform.salesposition.value == '') {
			alert('Please enter the area of interest.');
			return false;
		 }
		 if(document.contactform.salesregion.value == '') {
			alert('Please enter the desired region.');
			return false;
		 }
		 if(document.contactform.experienceselling.value == '') {
			alert('Please enter your experience selling recycling equipment.');
			return false;
		 }
		 if(document.contactform.productinterest.value == '') {
			alert('Please enter the GaiaRecycle product interest.');
			return false;
		 }
		 if((document.contactform.prefcontactso[0].checked == false) && (document.contactform.prefcontactso[[1].checked == false)) {
		 	alert('Please enter your Contact Preference for Sales/Referral Opportunities.');
			return false;
		 }
		 if((document.contactform.timecontactso[0].checked == false) && (document.contactform.timecontactso[1].checked == false)) {
			alert('Please enter your Contact Time Preference for Sales/Referral Opportunities.');
			return false;
		 }
		 return false;
	 }
	 if(document.contactform.type6.value) {
		if((document.contactform.prefcontactoi[0].checked == false) && (document.contactform.prefcontactoi[1].checked == false)) {
			alert('Please enter your Contact Preference for Other Information.');
			return false;
		 }
		if((document.contactform.timecontactoi[0].checked == false) && (document.contactform.timecontactoi[1].checked == false)) {
			alert('Please enter your Contact Time Preference for Other Information.');
			return false;
		 }
		 return false;
	 }
}
</script>

I think you were trying to write something like this

<script type="text/javascript">
    function validate_form() {
        if (document.contactform.firstname.value == '') {
            alert('Please enter your First Name.');
            return false;
        }
        if (document.contactform.lastname.value == '') {
            alert('Please enter your Last Name.');
            return false;
        }
        if (document.contactform.company.value == '') {
            alert('Please enter your Company.');
            return false;
        }
        if (document.contactform.address.value == '') {
            alert('Please enter your address.');
            return false;
        }
        if (document.contactform.city.value == '') {
            alert('Please enter your city.');
            return false;
        }
        if (document.contactform.state.value == '') {
            alert('Please enter your state.');
            return false;
        }
        if (document.contactform.zipcode.value == '') {
            alert('Please enter your zipcode.');
            return false;
        }
        if (document.contactform.country.value == '') {
            alert('Please enter your country.');
            return false;
        }
        if (document.contactform.workphone.value == '') {
            alert('Please enter your work phone.');
            return false;
        }
        if (document.contactform.email.value == '') {
            alert('Please enter your email.');
            return false;
        }
        if (document.contactform.website.value == '') {
            alert('Please enter your website.');
            return false;
        }
        if (document.contactform.type1.value) {
            if ((document.contactform.prefcontactcs[0].checked == false) && (document.contactform.prefcontactcs[1].checked == false)) {
                alert('Please enter your Contact Preference for Customer Support.');
                return false;
            }
            if ((document.contactform.timecontactcs[0].checked == false) && (document.contactform.timecontactcs[1].checked == false)) {
                alert('Please enter your Contact Time Preference for Customer Support.');
                return false;
            }
        }
        if (document.contactform.type2.value) {
            if (document.contactform.organicmaterial.value == '') {
                alert('Please enter the type of organic material.');
                return false;
            }
            if ((document.contactform.prefcontactps[0].checked == false) && (document.contactform.prefcontactps[[1].checked == false)) {
                alert('Please enter your Contact Preference for Project Information.');
                return false;
            }
            if ((document.contactform.timecontactps[0].checked == false) && (document.contactform.timecontactps[1].checked == false)) {
                alert('Please enter your Contact Time Preference for Project Information.');
                return false;
            }
        }
        if (document.contactform.type3.value) {
            if ((document.contactform.prefcontactpi[0].checked == false) && (document.contactform.prefcontactpi[1].checked == false)) {
                alert('Please enter your Contact Preference for Product Information.');
                return false;
            }
            if ((document.contactform.timecontactpi[0].checked == false) && (document.contactform.timecontactpi[1].checked == false)) {
                alert('Please enter your Contact Time Preference for Product Information.');
                return false;
            }
        }
        if (document.contactform.type4.value) {
            if (document.contactform.modelnum.value == '') {
                alert('Please enter the model number.');
                return false;
            }
            if (document.contactform.serialnum.value == '') {
                alert('Please enter the serial number.');
                return false;
            }
            if (document.contactform.purchasedate.value == '') {
                alert('Please enter the purchase date.');
                return false;
            }
            if (document.contactform.locationinstall.value == '') {
                alert('Please enter the install location.');
                return false;
            }
            if (document.contactform.usage.value == '') {
                alert('Please enter the machine usage.');
                return false;
            }
            if (document.contactform.wasterecycled.value == '') {
                alert('Please enter the type of organic waste recycled.');
                return false;
            }
            if ((document.contactform.companytype[0].checked == false) && (document.contactform.companytype[1].checked == false) && (document.contactform.companytype[2].checked == false) && (document.contactform.companytype[3].checked == false) && (document.contactform.companytype[4].checked == false) && (document.contactform.companytype[5].checked == false) && (document.contactform.companytype[6].checked == false) && (document.contactform.companytype[7].checked == false)) {
                alert('Please enter the company type.');
                return false;
            }
            if (document.contactform.contactperson.value == '') {
                alert('Please enter the contactperson.');
                return false;
            }
            if ((document.contactform.prefcontactts[0].checked == false) && (document.contactform.prefcontactts[[1].checked == false)) {
                alert('Please enter your Contact Preference for Technical Support.');
                return false;
            }
            if ((document.contactform.timecontactts[0].checked == false) && (document.contactform.timecontactts[1].checked == false)) {
                alert('Please enter your Contact Time Preference for Technical Support.');
                return false;
            }
        }
        if (document.contactform.type5.value) {
            if (document.contactform.salesposition.value == '') {
                alert('Please enter the area of interest.');
                return false;
            }
            if (document.contactform.salesregion.value == '') {
                alert('Please enter the desired region.');
                return false;
            }
            if (document.contactform.experienceselling.value == '') {
                alert('Please enter your experience selling recycling equipment.');
                return false;
            }
            if (document.contactform.productinterest.value == '') {
                alert('Please enter the GaiaRecycle product interest.');
                return false;
            }
            if ((document.contactform.prefcontactso[0].checked == false) && (document.contactform.prefcontactso[[1].checked == false)) {
                alert('Please enter your Contact Preference for Sales/Referral Opportunities.');
                return false;
            }
            if ((document.contactform.timecontactso[0].checked == false) && (document.contactform.timecontactso[1].checked == false)) {
                alert('Please enter your Contact Time Preference for Sales/Referral Opportunities.');
                return false;
            }
        }
        if (document.contactform.type6.value) {
            if ((document.contactform.prefcontactoi[0].checked == false) && (document.contactform.prefcontactoi[1].checked == false)) {
                alert('Please enter your Contact Preference for Other Information.');
                return false;
            }
            if ((document.contactform.timecontactoi[0].checked == false) && (document.contactform.timecontactoi[1].checked == false)) {
                alert('Please enter your Contact Time Preference for Other Information.');
                return false;
            }
        }
	return true;
    }
</script>

What did you change in the code? It looks the same to me. I re-pasted what you posted and it's still submitting the form even though none of the fields are filled out....

How do you call Javascript in your HTML? Your validation will let empty string (contains only white spaces) pass because you are checking on var=='' but not var==' '. You should trim all strings you are checking before you do the validation, or you intend to let empty string to pass?

This is the form tag I have in my html:

<form name="contactform" action="contactsubmit.php" onsubmit="return validate_form()" method="post">

The javascript is all in the header tag and called when the onsubmit fires.

How would you trim the strings? Javascript is not my forte.

To trim a string, you can use replace() function.

var a = "  abcde  "
var trim_a = a.replace(/^\s+|\s+$/g,"")  // return 'abcde'

In the code, you remove any leading or tailing white spaces from the string. You need the mode 'g' or you would not get the result correctly (when a string has white space both front and back).

Just from my quick look at your code, I am not sure about the way you check for radio button check.

// i.e.
document.contactform.prefcontactps[[1].checked == false

I think you need to do

!document.contactform.prefcontactps[[1].checked

The reason I have doubt is that the return value of document.contactform.prefcontactps[1].checked when unchecked could be 'undefined' instead of false. So 'undefined' is not equal to boolean false, and that it will pass your validation. Please let me know if I am wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.