Validation with Javascript

You can validate that all required fields are filled out before the form is submitted by adding some Javascript code to your page.

To add Javascript validation:

  1. Open the program that you use to edit your web page
  2. Within your <form> tag, add the onsubmit attribute:

    <form method="post" enctype="multipart/form-data" onsubmit="return validateForm(this);"
    action="http://https://forms.hush.com/yourformname">

  3. Above your <form> tag, add the validateForm Javascript function:
    <script language="javascript">
    function validateForm(form)
    {
    	var requiredFields = [
    		["Name", "Name"],
    		["Company_Name", "Company Name"],
    		["Office_Phone", "Office Phone"],
    		["hush_sender", "Email Address"],
    		["Mailing_Address", "Mailing Address"],
    		["City", "City"],
    		["State", "State"],
    		["Zip_Code", "Zip Code"],
    		["Country", "Country"],
    	];
    	
    	var emptyFields = [];
    
    	for (field in requiredFields)
    	{
    		var empty = form.elements[requiredFields[field][0]].value == "";
    		var name = requiredFields[field][1];
    
    		if (empty)
    		{
    			emptyFields[emptyFields.length] = name;
    		}
    	}
    
    	if (emptyFields.length > 0)
    	{
    		var msg = "The following fields can not be blank:\r\n\r\n";
    		for (field in emptyFields)
    		{
    			msg += "\t" + emptyFields[field] + "\r\n";
    		}
    		alert(msg);
    		return false;
    	}
    
    	return true;
    }
    </script>
    
    
  4. Customize the requiredFields list so that it matches the required fields in your form. On each line, the first parameter indicates the name of the field and the second indicates the label of the field.