Title: Bug in zip code
Last modified: April 20, 2020

---

# Bug in zip code

 *  Resolved [A.m](https://wordpress.org/support/users/ameliaakh/)
 * (@ameliaakh)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/bug-in-zip-code/)
 * There is an error when entering a postal code in Belgium. It is possible to enter
   an incorrect e.g. the Netherlands zip code (1011AM) and choose the country Belgium,
   no error will be displayed. In Belgium postal codes 1011AM are invalid but no
   error is displayed. How can this be corrected, is it theme related or is it woocommerce
   related?

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

 *  [Hari Shanker R](https://wordpress.org/support/users/harishanker/)
 * (@harishanker)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/bug-in-zip-code/#post-12705353)
 * Hi [@ameliaakh](https://wordpress.org/support/users/ameliaakh/)
 * I understand the problem that you’re talking about. Can you share more details
   about how to replicate the error? What was the zip code (or the format you entered)
   so as to get the error message? The reason why I asked is that
 * I was able to replicate the problem with the Netherlands zip code NOT showing
   an error when Belgium is chosen as the country though.
 * Now, WooCommerce has some basic postcode validation built-in to core. However,
   if you would like to implement something advanced, you might want to consider
   something like this:
    [https://woocommerce.com/products/postcodeaddress-validation/](https://woocommerce.com/products/postcodeaddress-validation/)
   [https://wordpress.org/plugins/woo-address-validator/](https://wordpress.org/plugins/woo-address-validator/)
 * I hope this helps!
 *  Thread Starter [A.m](https://wordpress.org/support/users/ameliaakh/)
 * (@ameliaakh)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/bug-in-zip-code/#post-12708847)
 * Hello [@harishanker](https://wordpress.org/support/users/harishanker/)
    “I was
   able to replicate the problem with the Netherlands zip code NOT showing an error
   when Belgium is chosen as the country though.”
 * Yes this is exactly the problem, postal code of Belgium consists of 4 digits 
   with no letters, there should be an error if someone uses a zip code that contains
   more than 4 digits. I think this is basic, if I choose Netherlands as country
   and will input 4 digits, it will show error, the same should be applied to Belgium,
   but in this case anything more than 4 digits should be an error. I think it’s
   not advanced? I would prefer to not make my shop heavier by adding more plugins
   Hari, is there a chance that WooCommerce will correct this for Belgium in near
   future?
    With kind regards, Amelia
 *  [Hari Shanker R](https://wordpress.org/support/users/harishanker/)
 * (@harishanker)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/bug-in-zip-code/#post-12727571)
 * Hi [@ameliaakh](https://wordpress.org/support/users/ameliaakh/)
 * I hear you. It’s a bummer that the Belgium zip/postal code validation is not 
   available in the WooCommerce core by default.
 * I checked WooCommerce core and found the relevant lines of code that adds this
   feature: [https://github.com/woocommerce/woocommerce/blob/d20d429e3264798fbfd5335e971f1e46531cf7bb/includes/class-wc-validation.php#L40-L108](https://github.com/woocommerce/woocommerce/blob/d20d429e3264798fbfd5335e971f1e46531cf7bb/includes/class-wc-validation.php#L40-L108)
 * As seen [here](https://github.com/woocommerce/woocommerce/blob/d20d429e3264798fbfd5335e971f1e46531cf7bb/includes/class-wc-validation.php#L107),
   the `woocommerce_validate_postcode` hook can be used to manipulate these validation
   rules.
 * Based on this, I prepared a quick code snippet that should help you in achieving
   your requirement:
 *     ```
       add_filter('woocommerce_validate_postcode','woo_validate_belgium_postcode',10,3);
   
       function woo_validate_belgium_postcode($valid, $postcode, $country){
           if($country=="BE")
                   $valid = (bool) preg_match( '/^([0-9]{4})$/', $postcode );
         // Checks your postcode to seee if it matches belgium (4 digits). If not, it will reject validation.
         return $valid;
       }
       ```
   
 * I’d recommend adding it to your site using a [Code Snippets Plugin](https://wordpress.org/plugins/code-snippets/),
   even though you can always add code snippets like this to the bottom of your 
   theme’s `functions.php`. (The downside of this approach is that your code gets
   lost in plugin/theme updates).
 * When you add this PHP code to your site, WooCommerce will only accept four-digit
   zip codes when you select Belgium as the country. As you can see in the following
   screenshot, it rejects the `1011AM` zip code from your example: [https://d.pr/i/J3ci41](https://d.pr/i/J3ci41)
 * I hope this helps.
    -  This reply was modified 6 years, 1 month ago by [Hari Shanker R](https://wordpress.org/support/users/harishanker/).
 *  Thread Starter [A.m](https://wordpress.org/support/users/ameliaakh/)
 * (@ameliaakh)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/bug-in-zip-code/#post-12728236)
 * [@harishanker](https://wordpress.org/support/users/harishanker/) Thank you very
   much Hari, this is so nice. I added the code and it works! Thanks again for your
   help!
    Wish you a great day, Amelia

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

The topic ‘Bug in zip code’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [Postal Code](https://wordpress.org/support/topic-tag/postal-code/)

 * 4 replies
 * 2 participants
 * Last reply from: [A.m](https://wordpress.org/support/users/ameliaakh/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/bug-in-zip-code/#post-12728236)
 * Status: resolved