Title: filter on the postcode (ZIP code)
Last modified: August 22, 2016

---

# filter on the postcode (ZIP code)

 *  Resolved [Alnatroz75018](https://wordpress.org/support/users/alnatroz75018/)
 * (@alnatroz75018)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/)
 * Dear team,
    I have tried hard to put a filter on a postcode in the CF7.
 * The idea is to display a message when the postcode is not in the area we operate
   in.
 * I used the following code in the text.php file (as you can guess I am pretty 
   new in PHP and only customed what I could find on the web)
 * This doesn’t work and drives me crazy. Any help would be appreciated.
 * The postcode is a text on the form and has the id:postcode
    this line in the 
   form: <p>[text*postcode /4 id:postcode placeholder “postcode”]<p>
 * The following code is in the text.php in C:\wamp\www\wordpress\wp-content\plugins\
   contact-form-7\modules
 *     ```
       add_filter('wpcf7_validate_text','postcode_custom_filter', 10, 2); // Email field
       add_filter('wpcf7_validate_text*', 'postcode_custom_filter', 10, 2); // Req. Email field
       // Add custom validation for CF7 form fields on postcode
   
       function postcode_in_area($postcode){ // check if the postcode is in our area
       	if(
       		preg_match('2010', $postcode) ||
       		preg_match('2011', $postcode) ||
       		preg_match('2013', $postcode) ||
       		preg_match('2014', $postcode) ||
       		preg_match('2015', $postcode) ||
       		preg_match('2016', $postcode) ||
       		preg_match('2017', $postcode) ||
       		preg_match('2018', $postcode) ||
       		preg_match('2019', $postcode)
       	){
       		return true; // It's an area we operate in
       	}else{
       		return false; // It's an area we do NOT service
       	}
       }
   
       function postcode_custom_filter($result,$tag){
       	$type = $tag['type'];
       	$name = $tag['name'];
       	if('postcode' == $type){ // Only apply to fields with the form field name of "Your-Postcode"
       		$the_value = $_POST[$name];
       		if( postcode_in_area($the_value)){ // Isn't a company email address (it matched the list of free email providers)
       			$result['valid'] = false;
       			$result['reason'][$name] = "sorry we do not operate in this area";
       		}
       	}
       	return $result;
       }
       ```
   
 * [https://wordpress.org/plugins/contact-form-7/](https://wordpress.org/plugins/contact-form-7/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647272)
 * It’s close, but this
 *     ```
       if('postcode' == $type){
       ```
   
 * always results _false_ because the `$type` is “text*” in the case.
 * The `$name` is “postcode” (a name is the second word in a form-tag, a type is
   the first one) so change the line to:
 *     ```
       if('postcode' == $name){
       ```
   
 * Also, you shouldn’t edit plugin files. You can customize in another place like
   your theme’s functions.php.
 *  Thread Starter [Alnatroz75018](https://wordpress.org/support/users/alnatroz75018/)
 * (@alnatroz75018)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647287)
 * Dear Takayuki,
    thank you for looking into my issue.
 * I have done the modification (this time in the plugin editor in WP)
 * Unfortunatly it does not seem to work. whatever I populate in the postcode field,
   the circular arrows keep turning and nothing happens
 * the modified code is the following
 *     ```
       add_filter('wpcf7_validate_text','postcode_custom_filter', 10, 2); // postcode field
       add_filter('wpcf7_validate_text*', 'postcode_custom_filter', 10, 2); // Req. postcode field
       // Add custom validation for CF7 form fields on postcode
   
       function postcode_in_area($postcode){ // check if the postcode is in our area
       	if(
       		preg_match('2010', $postcode) ||
       		preg_match('2011', $postcode) ||
       		preg_match('2013', $postcode) ||
       		preg_match('2014', $postcode) ||
       		preg_match('2015', $postcode) ||
       		preg_match('2016', $postcode) ||
       		preg_match('2017', $postcode) ||
       		preg_match('2018', $postcode) ||
       		preg_match('2019', $postcode)
       	){
       		return true; // It's an area we operate in
       	}else{
       		return false; // It's an area we do NOT service
       	}
       }
   
       function postcode_custom_filter($result,$tag){
       	$type = $tag['type'];
       	$name = $tag['name'];
       	if('postcode' == $name){ // Only apply to fields with the form field name of "postcode"
       		$the_value = $_POST[$name];
       		if( postcode_in_area($the_value)){ // Is not in our area
       			$result['valid'] = false;
       			$result['reason'][$name] = "sorry we do not operate in this area";
       		}
       	}
       	return $result;
       }
       ```
   
 *  Thread Starter [Alnatroz75018](https://wordpress.org/support/users/alnatroz75018/)
 * (@alnatroz75018)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647292)
 * Hi again,
 *  I believe the error comes from “preg_match”, as it is probably not the best 
   way to check the exact value of the postcode. Is there a way to have a list of
   postcodes and match the result entered in the form against these values?
 * Thank you in advance
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647297)
 * Use [in_array](http://php.net/manual/en/function.in-array.php).
 *  Thread Starter [Alnatroz75018](https://wordpress.org/support/users/alnatroz75018/)
 * (@alnatroz75018)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647312)
 * thank you Takayuki, I used the following code if this can help someone and it
   works
 *     ```
       add_filter('wpcf7_validate_text','postcode_custom_filter', 10, 2); // postcode field
       add_filter('wpcf7_validate_text*', 'postcode_custom_filter', 10, 2); // Req. postcode field
       // Add custom validation for CF7 form fields on postcode
   
       function postcode_in_area($postcode){ // check if the postcode is in our area
       	$areaArray = array('75001', '75002', '75003', '75004');
   
           if (!in_array($postcode, $areaArray)) {
                   return true; // It's an area we operate in
       	}else{
       		return false; // It's an area we do NOT service
       	}
       }
   
       function postcode_custom_filter($result,$tag){
       	$type = $tag['type'];
       	$name = $tag['name'];
       	if('postcode' == $name){ // Only apply to fields with the form field name of "postcode"
       		$the_value = $_POST[$name];
       		if( postcode_in_area($the_value)){ // Is not in our area
       			$result['valid'] = false;
       			$result['reason'][$name] = "Sorry, we do not operate in this area.";
       		}
       	}
       	return $result;
       }
       ```
   
 *  [chuey2u](https://wordpress.org/support/users/chuey2u/)
 * (@chuey2u)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647637)
 * Alnotorz –
    I’m trying to incorporate the same function into my website but I’m
   having a hard time to get the coding to work. Can you walk me through what you
   did to get the coding to work?
 *  [Sciron](https://wordpress.org/support/users/sciron/)
 * (@sciron)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647647)
 * I’ve add your code Alnatroz.
    But he will not check my submitted postcode.
 * Do you have any suggestion?

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘filter on the postcode (ZIP code)’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [postcode](https://wordpress.org/support/topic-tag/postcode/)
 * [values](https://wordpress.org/support/topic-tag/values/)

 * 7 replies
 * 4 participants
 * Last reply from: [Sciron](https://wordpress.org/support/users/sciron/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/filter-on-the-postcode-zip-code/#post-5647647)
 * Status: resolved