# Validating Zip for Single Line Fields

**URL:** <https://community.gravityforms.com/t/validating-zip-for-single-line-fields/12991>\
**Category:** Get Help\
**Tags:** gform\_field\_validation, zip-code\
**Created:** [July 7, 2022, 7:28pm UTC](https://community.gravityforms.com/t/validating-zip-for-single-line-fields/12991 "2022-07-07T19:28:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![user608ed7c3b1243571](https://avatars.discourse-cdn.com/v4/letter/u/47e85d/32.png) [@user608ed7c3b1243571](https://community.gravityforms.com/u/user608ed7c3b1243571)\
**Post date:** [July 7, 2022, 7:28pm UTC](https://community.gravityforms.com/t/validating-zip-for-single-line-fields/12991/1 "2022-07-07T19:28:31Z")

</div>

My coding abilities are very limited - I apologize if this is an obvious answer.

I have three zip fields on 2 forms that need to allow only 5 numbers.

I took code from here - [Zipcode validation for Gravity Forms (5 digits, 2 approaches). · GitHub](https://gist.github.com/hiranthi/b23884c144c5000db535b19a38c0a2f6) and try to adapt it to single line fields, however when I add the code, it applies the filter to every single line field on my forms.

How do I get it to just validate the zip on single line fields with the IDs 17, 43, 63 ?

```auto
$forms = array( '2', '4' );

# looping through the array to add an 'add_filter' for each
foreach ( $forms as $i => $form )
    add_filter( "gform_field_validation_{$form}", 'custom_zip_validation', 10, 4 );

# the function
function custom_zip_validation( $result, $value, $form, $field )
{
	if ( ( 'address' == $field->type ) && $result['is_valid'] )
	{	
		$zip_value = rgar( $value, $field->id . '17, 43, 63' );

		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;
} // end custom_zip_validation

```

---

<div class="post-metadata">

**Author:** ![sacom](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/sacom/32/53_2.png) [@sacom](https://community.gravityforms.com/u/sacom)\
**Post date:** [July 8, 2022, 1:14pm UTC](https://community.gravityforms.com/t/validating-zip-for-single-line-fields/12991/2 "2022-07-08T13:14:27Z")

</div>

You can use this simpler function:

```auto
function text_zip_validation( $result, $value, $form, $field ) {
	if ( ! ctype_digit( $value ) || 5 != strlen( $value ) ) {
		$result['is_valid'] = false;
		$result['message'] = 'Please check your Zip Code. It must be 5 digits only.';
	}
	return $result;
}

```

Then add as many add\_filter lines as follows:

```auto
add_filter( 'gform_field_validation_xx_yy', 'text_zip_validation', 10, 4 );

```

Where xx is your form id and yy your field id. For example if you want to apply the validation function to field id 17 in a form that has id 2, you would add this line:

```auto
add_filter( 'gform_field_validation_2_17', 'text_zip_validation', 10, 4 );

```

And just duplicate the line and update the form id and field id to validate other fields.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [August 7, 2022, 1:14pm UTC](https://community.gravityforms.com/t/validating-zip-for-single-line-fields/12991/3 "2022-08-07T13:14:58Z")

</div>

Closed on August 7, 2022, 1:14pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
