Validate Gravity Forms Address Fields

GravityForms

How to Validate Gravity Forms Address Fields:

Recently, the need to validate the zip code in a standard address field using Gravity Forms arose for a client. Following the documentation provided for developers with the most recent to date paid Commercial Version, we set up a zip code validation. Seemed easy enough.

The problem:

The method noted validates the whole field, which consists of:

1. Street Address
2. Address Line 2
3. City
4. State
5. Zip Code
6. Country (Note: In this example, we are using a US address field, so we do not bother with Country).
validate gravity forms

What does “The method noted validates the whole field” mean? It means if we set a single validation rule for just the zip code, while we can validate the zip code, we are validating the whole address field. So, if we properly enter a 5 digit zip code and miss any of the others, the form validates!

In this case, we don’t just save the entry, we send some parts of it to a third party API also and if we do not send a 5 digit zip code, it fails.

First, please know that in the code and example below, we not only do not need to worry about the Country, but as far as the Third Party API is concerned, “Address Line 2” is optional, not required, nor is it normally required in forms with addresses.

So we need only concern ourselves with:

1. Street Address
3. City
4. State
5. Zip Code

Now, remember back in school when your math teacher discussed Combinations? In our case there are 16 combinations for what we need to check for:

1 3 4 5
T T T T 0 All 4 entered are valid
F F F F 1 All other cases are invalid
F T T T 2
T F T T 3
T T F T 4
T T T F 5
F F T T 6
F T F T 7
F T T F 8
T F F T 9
T F T F 10
T T F F 11
F F F T 12
T F F F 13
F T F F 14
F F T F 15

And below is the code added to the Child Theme functions.php file (The Form ID is 7 and the Field is 3):

<?php

//YOUR OTHER PHP GOES HERE...ADD BELOW TO BOTTOM OF FUNCTION.PHP AND MAKE SURE THE FILE STARTS WITH AND ENDS WITH FIRST AND LAST LINE IN THIS EXAMPLE.

/*** This forces the zip code field in Form 7 Field 3 to be 5 digits and insures we have entered Street Address, City and State also ***
 target form 7, field 3 (address field on the form) */
 
 add_filter("gform_field_validation_7_3", "custom_address_validation_7_3", 10, 4);
 function custom_address_validation_7_3($result, $value, $form, $field){
 //address field will pass $value as an array with each of the elements as an item within the array, the key is the field id
 //do custom validation
 $l1 = $value["3.1"]; //Street Address
 $l3 = $value["3.3"]; //City
 $l4 = $value["3.4"]; //State
 $zip = $value["3.5"]; //Zip Code
 
 //check to see if the values you care about are valid
 
 //all are valid 1T, 3T, 4T, 5T
 
 if (((ctype_digit($zip)) && (strlen($zip) == 5)) && ($l1 !== "") && ($l3 !== "") && ($l4 !== "")) {
 $result["is_valid"] = true;
 $result["message"] = ""; 
 }else{
 
 //If ALL 4 not entered or zip not valid 1F, 3F, 4F, 5F
 if ((!((ctype_digit($zip)) && (strlen($zip) == 5))) && ($l1 == "") && ($l3 == "") && ($l4 == "")) { //1F, 3F, 4F, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address, a City, a State and a valid 5 digit zip code."; 
 } 
 
 // One Falses = 4
 
 //1F
 if (($l1 == "") && ($l3 !== "") && ($l4 !== "")&& ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1F, 3T, 4T, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address."; 
 }
 //3F
 if (($l1 !== "") && ($l3 == "") && ($l4 !== "") && ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1T, 3F, 4T, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please enter a City."; 
 }
 //4F
 if (($l1 !== "") && ($l3 !== "") && ($l4 == "") && ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1T, 3T, 4F, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please select a State."; 
 }
 //5F
 if (($l1 !== "") && ($l3 !== "") && ($l4 !== "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1T, 3T, 4T, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a valid numeric 5 digit zip code."; 
 }
 
 // Two Falses = 6
 
 //1F & 3F
 if (($l1 == "") && ($l3 == "") && ($l4 !== "") && ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1F, 3F, 4T, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address and a City."; 
 }
 
 //1F & 4F
 
 if (($l1 == "") && ($l3 !== "") && ($l4 == "") && ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1F, 3T, 4F, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address and a State."; 
 }
 
 //1F & 5F
 if (($l1 == "") && ($l3 !== "") && ($l4 !== "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1F, 3T, 4T, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address and a valid numeric 5 digit zip code.";
 }

 //3F & 4F
 if (($l1 !== "") && ($l3 == "") && ($l4 == "") && ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1T, 3F, 4F, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please enter a City and a State.";
 }
 
 //3F & 5F
 if (($l1 !== "") && ($l3 == "") && ($l4 !== "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1T, 3F, 4T, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a City and a valid numeric 5 digit zip code.";
 }
 
 //4F & 5F
 if (($l1 !== "") && ($l3 !== "") && ($l4 == "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1T, 3T, 4F, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a State and a valid numeric 5 digit zip code.";
 }
 
 // Three Falses = 4
 
 //1F & 3F & 4F
 
 if (($l1 == "") && ($l3 == "") && ($l4 == "") && ((ctype_digit($zip)) && (strlen($zip) == 5))) { //1F, 3F, 4F, 5T
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address, a City and a State.";
 }
 
 //3F & 4F & 5F
 
 if (($l1 !== "") && ($l3 == "") && ($l4 == "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1T, 3F, 4F, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a City, a State and a valid numeric 5 digit zip code.";
 }
 
 //1F & 4F & 5F
 
 if (($l1 == "") && ($l3 !== "") && ($l4 == "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1F, 3T, 4F, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address, a State and a valid numeric 5 digit zip code.";
 }
 
 //1F &3F & 5F
 
 if (($l1 == "") && ($l3 == "") && ($l4 !== "") && (!((ctype_digit($zip)) && (strlen($zip) == 5)))) { //1F, 3F, 4T, 5F
 $result["is_valid"] = false;
 $result["message"] = "Please enter a Street Address, a City and a valid numeric 5 digit zip code.";
 } 
 } 
 return $result;
 }
 ?>

We have another set of validation rules in place. Those do two things: strip out any unwanted special characters, replace them with a space (and replace double spaces too), then truncate fields that are too long. That is a post for another day.

If you need help with Gravity Forms, please Contact Pioneer Web Design.