# Zip Code Field Validation

**URL:** https://community.gravityforms.com/t/zip-code-field-validation/869
**Category:** Get Help
**Tags:** gform\_field\_validation
**Created:** [January 23, 2019, 3:48pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869 "2019-01-23T15:48:53Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 3:48pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/1 "2019-01-23T15:48:53Z")

</div>

I’m using the Address field in my forms so there is no way of setting a mask for the individual sub fields in the Address field. I need to validate the zip code field such that, upon submission, if the field contains anything other than 5 numerals, the form should show an error message just like any other required field. I’m using United States address format and no need for the zip+4 format.

I added the [https://docs.gravityforms.com/gform\_field\_validation/#3-address-field-validation](https://docs.gravityforms.com/gform_field_validation/#3-address-field-validation) filter that mentions zip code validation to my functions.php file (see below), but this has no effect; the form will still accept and submit non-numeric values and strings longer than 5 caharcters. Any idea what I’m missing?

my functions.php (the zip code field in question is on form ID3 and is field 55.5

```auto
<?php
/*================================================
#1 Load the styles 
================================================*/
function dc_enqueue_styles() {
	wp_enqueue_style( 'divi-parent', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'dc_enqueue_styles' );

add_filter( 'gform_field_validation_3_55.5', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            '123',
            '456'
        );
 
        $zip_value = rgar( $value, $field->id . '55.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $result['is_valid'] = false;
            $result['message'] = 'Zip validation failed.';
        }
    }
 
    return $result;
}

```

---

<div class="post-metadata">

### Author: ![hiranthi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/hiranthi/32/359_2.png) [@hiranthi](https://community.gravityforms.com/u/hiranthi)
#### Post date: [January 23, 2019, 3:57pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/2 "2019-01-23T15:57:35Z")

</div>

I see you’ve used `gform_field_validation_3_55.5` as the filter, have you tried `gform_field_validation_3_55`?

From what I see in the docs (and from what I’ve used myself in the past) it doesn’t work with the subID for the specific field inside a field-group and should be Form ID + Field ID. So the overall field ID (55 in this case).

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 4:05pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/3 "2019-01-23T16:05:21Z")

</div>

I got it. You were right about gform\_field\_validation\_3\_55; then I referenced the subfield .5 below and it worked:

$zip\_value = rgar( $value, $field-\>id . ‘.5’ );

Thanks!

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 4:10pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/4 "2019-01-23T16:10:53Z")

</div>

I spoke too soon. The code now throws the error, but replacing the zip with a 5-digit string still continues to throw the same validation error.

---

<div class="post-metadata">

### Author: ![hiranthi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/hiranthi/32/359_2.png) [@hiranthi](https://community.gravityforms.com/u/hiranthi)
#### Post date: [January 23, 2019, 4:22pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/5 "2019-01-23T16:22:07Z")

</div>

Well, that’s one step closer to the solution either way 😉

In the code you pasted above, the ‘demo zips’ are still mentioned (which are both 3 numbers). Is that the code you’re indeed using? Because that situation doesn’t resemble what you’re trying to achieve.

You could use [ctype\_digit](http://php.net/manual/en/function.ctype-digit.php) + [strlen](http://php.net/manual/en/function.strlen.php) inside the filter to check if the given value is indeed (5) digits:

```
if ( ! ctype_digit( $zip_value ) || 5 != strlen( $zip_value ) ) 
{
    $result['is_valid'] = false; 
    $result['message'] = 'Zip validation failed.'; 
}
```

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 5:31pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/6 "2019-01-23T17:31:48Z")

</div>

> [@hiranthi](#):
>
> if ( ! ctype\_digit( $zip\_value ) || 5 != strlen( $zip\_value ) ) { $result[‘is\_valid’] = false; $result[‘message’] = ‘Zip validation failed.’; }

So like this:

> add\_filter( ‘gform\_field\_validation\_3\_55’, ‘custom\_zip\_validation’, 10, 4 );  
> function custom\_zip\_validation( $result, $value, $form, $field ) {  
> if ( $result[‘is\_valid’] ) {
> 
> ```
> $zip_value = rgar( $value, $field->id . '.5' );
> 
> if ( ! ctype_digit( $zip_value ) || 5 != strlen( $zip_value ) ) 
> 
> ```
> 
> {  
> $result[‘is\_valid’] = false;  
> $result[‘message’] = ‘Zip validation failed.’;  
> }  
> }
> 
> ```
> return $result;
> 
> ```
> 
> }

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 5:54pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/7 "2019-01-23T17:54:14Z")

</div>

That worked, but what if I want to use the same filter for more than one form on my site? I repeated the filter and updated the form but with both filters in my functions.php, the site was not accessible. What would the syntax look like for two forms / 2 field IDs?

---

<div class="post-metadata">

### Author: ![hiranthi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/hiranthi/32/359_2.png) [@hiranthi](https://community.gravityforms.com/u/hiranthi)
#### Post date: [January 23, 2019, 6:16pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/8 "2019-01-23T18:16:33Z")

</div>

Then you’d add a seperate add\_filter for that form + field ID (and I’d select it at field type address inside the function instead of the field ID to keep it compact & more scalable).

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 6:39pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/9 "2019-01-23T18:39:15Z")

</div>

So would the syntax look like this if the two forms are ID 6 and ID 7 with both having the zip field at 15.5?

add\_filter( ‘gform\_field\_validation\_6\_15’, ‘gform\_field\_validation\_7\_15’, ‘custom\_zip\_validation’, 10, 4 );  
function custom\_zip\_validation( $result, $value, $form, $field ) {  
if ( $result[‘is\_valid’] ) {

```
    $zip_value = rgar( $value, $field->id . '.5' );

    if ( ! ctype_digit( $zip_value ) || 5 != strlen( $zip_value ) ) 

```

{  
$result[‘is\_valid’] = false;  
$result[‘message’] = ‘Please check your Zip Code. It must be 5 digits only.’;  
}  
}

```
return $result;

```

}

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 7:09pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/10 "2019-01-23T19:09:03Z")

</div>

I tried the above and it doesn’t work. So I’m not sure how to have the same filter run on multiple forms.

---

<div class="post-metadata">

### Author: ![hiranthi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/hiranthi/32/359_2.png) [@hiranthi](https://community.gravityforms.com/u/hiranthi)
#### Post date: [January 23, 2019, 7:58pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/11 "2019-01-23T19:58:21Z")

</div>

The add\_filter should be used once per form. The first string is the filtername, the second string is the name of the function that should be used. In yours the first two strings are filter names and the third is the functionname. Since the second string is recognized by WordPress as the function and that function doesn’t exist: it doesn’t work.

I’ve added a gist with the two approaches I was referring to earlier: [https://gist.github.com/hiranthi/b23884c144c5000db535b19a38c0a2f6](https://gist.github.com/hiranthi/b23884c144c5000db535b19a38c0a2f6)

Perhaps that clarifies things a bit 🙂

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 23, 2019, 8:29pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/12 "2019-01-23T20:29:19Z")

</div>

Thanks again!

---

<div class="post-metadata">

### Author: ![KappKoncepts](https://avatars.discourse-cdn.com/v4/letter/k/a9a28c/32.png) [@KappKoncepts](https://community.gravityforms.com/u/KappKoncepts)
#### Post date: [January 29, 2019, 5:13pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/13 "2019-01-29T17:13:13Z")

</div>

Hi,

I am also looking to implement this solution. The only difference for me is I have several ‘zip’ fields on the same form.

How can I add the filter to every ‘Zip’ field on the form?

Thanks

---

<div class="post-metadata">

### Author: ![jcallanan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jcallanan/32/365_2.png) [@jcallanan](https://community.gravityforms.com/u/jcallanan)
#### Post date: [January 29, 2019, 5:34pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/14 "2019-01-29T17:34:37Z")

</div>

Yes - I believe all you need to do is add an additional line like:  
add\_filter( ‘gform\_field\_validation\_3\_55’, ‘custom\_zip\_validation’, 10, 4 ); for each zip field in the form (in this example, ‘3’ is form ID and ‘55’ is the field ID of address field) replacing the " \_55" with the field ID of additional address field.

If you are using a field for a zip code as a stand-alone and not part pf a address field, the function itself might be a little different.

---

<div class="post-metadata">

### Author: ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)
#### Post date: [January 29, 2019, 6:35pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/15 "2019-01-29T18:35:15Z")

</div>

Actually, [Hiranthi’s code here](https://gist.github.com/hiranthi/b23884c144c5000db535b19a38c0a2f6) accounts for multiple forms. You can add multiple form IDs on line form 4.

`$forms = array( '6', '7' );`

---

<div class="post-metadata">

### Author: ![KappKoncepts](https://avatars.discourse-cdn.com/v4/letter/k/a9a28c/32.png) [@KappKoncepts](https://community.gravityforms.com/u/KappKoncepts)
#### Post date: [January 29, 2019, 7:31pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/16 "2019-01-29T19:31:13Z")

</div>

@chrishajer sorry but her solution is for multiple forms. I only have 1 form with 24 Address fields that need zip code validation.

---

<div class="post-metadata">

### Author: ![KappKoncepts](https://avatars.discourse-cdn.com/v4/letter/k/a9a28c/32.png) [@KappKoncepts](https://community.gravityforms.com/u/KappKoncepts)
#### Post date: [January 29, 2019, 7:34pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/17 "2019-01-29T19:34:34Z")

</div>

@jcallanan Thank you. I guess I was looking for a more simpler solution rather then write ‘add\_filter’ 24 times. I have 24 address fields and each of the zip codes need validation.

Will there be any issues if I write 24 ‘add\_filter’ hooks?

---

<div class="post-metadata">

### Author: ![hiranthi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/hiranthi/32/359_2.png) [@hiranthi](https://community.gravityforms.com/u/hiranthi)
#### Post date: [January 29, 2019, 7:51pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/18 "2019-01-29T19:51:59Z")

</div>

Actually that’s quite possible, just remove the field ID (to apply it to a single form) and use the first gist where the ‘address’-field type is mentioned.

The add\_filter would just be added once for the whole form and all address-fields in that form would have that same filter for the zipcode field.

Anyway, if you check the gist for zipcode-3.php (just added it) you’ll see what I mean 🙂

---

<div class="post-metadata">

### Author: ![KappKoncepts](https://avatars.discourse-cdn.com/v4/letter/k/a9a28c/32.png) [@KappKoncepts](https://community.gravityforms.com/u/KappKoncepts)
#### Post date: [January 29, 2019, 8:22pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/19 "2019-01-29T20:22:04Z")

</div>

@hiranthi You are a genius. I saw the gist but never really pay attention to the zipcode-2.php 🤦🏽‍♂️

I owe you a cup of coffee for this one ☕

Cheers!

---

<div class="post-metadata">

### Author: ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)
#### Post date: [January 29, 2019, 10:16pm UTC](https://community.gravityforms.com/t/zip-code-field-validation/869/21 "2019-01-29T22:16:41Z")

</div>

> [@KappKoncepts](#):
>
> @chrishajer sorry but her solution is for multiple forms. I only have 1 form with 24 Address fields that need zip code validation.

Oops sorry, my mistake.

[Next page](https://community.gravityforms.com/t/zip-code-field-validation/869.md?page=2)
