Welcome

Pioneer Valley Web Design is your local source for Website Design and Development for you or your small business. Services include:

Website Hosting.

Website Management.

Website Maintenance.

Search Engine Optimization.

Pioneer Valley Web Design
WordPress

Pioneer Valley Web Design Specializes in:

Theme Customization, Child Themes, Custom Page Templates
Custom PHP
Pioneer Valley Web Design

Complex Forms
Complex User Registration
NewsLetters
Site Repairs including compromised sites
Site Rebuilds from other Applications to WordPress

Please Contact Us or call 603-583-3777
M-F 9AM-7PM Sa 10AM-6PM and Su 12PM-6PM.

Pioneer Valley Web Design is located in Montague, MA and has the experience and know how to get your small business or profession online and searchable at a very reasonable rate. Please feel free to use the form below to Contact Pioneer Valley Web Design about your web design and/or hosting needs. A written quote will be given in a timely manner, usually within 1 business day.

Recent Posts

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 Valley Web Design.

  1. That Stubborn WordPress Custom Post Type Leave a reply
  2. Use State Abbreviations : Gravity Forms 2 Replies
  3. Zapier and Gravity Forms Leave a reply
  4. Happy Holidays from Pioneer Valley Web Design Leave a reply
  5. NSA Surveillance Breakdown Leave a reply
  6. A Child Category Sub-Menu Leave a reply
  7. How to Set Up Browser Caching with WordPress Leave a reply
  8. Major Update to Google Maps Is Here Leave a reply
  9. Using phpMyAdmin To Get Registered Users Email Addresses Leave a reply
  10. Chris Hadfield – Space Oddity Leave a reply
  11. Redirect Search Query in WordPress Leave a reply
  12. Using WordPress To Block Comment Spam Leave a reply
  13. A Boy And His Atom Leave a reply
  14. Mars Rover Curiosity 360 Panorama Leave a reply
  15. Windows 8 RTM Leave a reply
  16. Curiosity Lands on Mars Leave a reply
  17. Curiosity Leave a reply
  18. Transit of Venus Leave a reply
  19. Jupiter, Moon, Venus Leave a reply
  20. Keeping Windows Running Cleanly Each Day Leave a reply
  21. Windows 8 Developer Preview Leave a reply
  22. STS-135 and A Farewell Leave a reply
  23. Windows 8 Preview Leave a reply
  24. Discovery’s Final Orbit Leave a reply
  25. The Coveted Service Pack 1 Leave a reply
  26. SPAM 2011 (Same Advise) Leave a reply
  27. Finkbeiner Bubbles Leave a reply
  28. Photo Fuse Leave a reply
  29. Java Exploits Increase – How to Properly Update Java Leave a reply
  30. Prizefight! Leave a reply
  31. The EN-V Project Leave a reply
  32. Windows 7 WMC and DVD Subtitles Leave a reply
  33. Windows 7 – get it now Leave a reply
  34. Windows 7 Enterprise Tests Underway Leave a reply
  35. Outlook Express Grinds to a Halt Leave a reply
  36. Windows Live Tips and Tricks Leave a reply
  37. 10 Great Windows Sites (to grow) Leave a reply
  38. Vista SP2 is here! Leave a reply
  39. XP SP3 Leave a reply
  40. Windows 7 RC Leave a reply
  41. PC Safety Leave a reply
  42. Windows 7 is here! Windows 7 is here! Leave a reply
  43. The Mojave Experiment Leave a reply
  44. Block those Domains Leave a reply
  45. Vista SP1 Streaming Music From Windows Home Server Leave a reply
  46. McAfee Says Leave a reply
  47. A Win, Win with Windows Leave a reply
  48. Half Life II on Vista SP1 Leave a reply
  49. Tech Leaders Leave a reply
  50. Software Inspectors Leave a reply
  51. Reduce SPAM now Leave a reply
  52. National Cyber Alert System Leave a reply
  53. Windows Vista SP1 Leave a reply