Tuesday, August 9, 2011

Required fields in Address Two Contact forms

Here is a simple JavaScript field validation that you can use on your address two contact forms. You will need an Address Two contact form and a little HTML know how.

Step 1 - give the form a Name(A2Form) and add the onsubmit call.
Change:

<form action="http://www.addresstwo.com/cf/function_addaccount.asp" method="post" style="padding: 0px;" target="_top">

To:

<form name="A2Form" onsubmit="return validateForm()" action="http://www.addresstwo.com/cf/function_addaccount.asp" method="post" style="padding: 0px;" target="_top">


Step 2 -add the validation script into the <head> section of your page.

<script type="text/javascript">
function validateForm() {
// repeat the following for each required field
     var x = document.forms["A2Form"]["FieldNameHere"].value;
     if (x == null || x == "") {
     alert("message here - Field Required ");
     return false;
     }
// end repeating code for each required field
}
</script>


Special Note: Validating Checkboxes
Checkboxes and radio buttons are a special breed of input that you cannot validate based simply on the value attribute.  Instead, the code for a required checkbox input would look like this:

     var x = document.forms["A2Form"]["FieldNameHere"].checked;
     if (x != true) {
     alert("message here - Field Required ");
     return false;
     }


You can see this script in action at http://www.w3schools.com/js/js_form_validation.asp

Happy Validating!

No comments:

Post a Comment