Johny22's avatar

Postal Code Validation

Hi can you recommend me some tested zip validation package?

0 likes
7 replies
fylzero's avatar
fylzero
Best Answer
Level 67

@johny-22 Validating just zip/postal codes is a bit tricky and a moving target. You can write a ton of regex / use various APIs to do so and it still won't ever be perfect. If address accuracy is important to your project I'd suggest using Google Place Autocomplete.

https://developers.google.com/maps/documentation/places/web-service/autocomplete

You've probably seen this used in other apps, basically when you enter and select an address the postal code is grabbed for you.

https://www.google.com/amp/s/www.positronx.io/create-google-autocomplete-address-in-laravel/amp/

CorvS's avatar

@johny-22 Are we talking international postal codes or is your app going to be limited to certain countries/regions?

Johny22's avatar

It will be limited to certain countries

CorvS's avatar

@johny-22 Then you could either follow @fylzero 's advise or check the possible structure of your zip codes (numeric, alphanumeric, with/wo special characters, ...) for each country and create a validation rule accordingly (if the number of supported countries is managable). You should also keep in mind that using Google's autocomplete API is gonna cost you around 2-3 bucks / 1000 requests.

martinbean's avatar

It will be limited to certain countries

@johny-22 And those countries are…?

C’mon. You have to give us more information if you’re expecting helpful answers.

Johny22's avatar

Oh sorry, of course. Germany, Poland, Austria, Belgium, Denmark, France, Croatia, Luxembourg, Hungary, Slovenia, Slovakia, Czech Republic, Finland, Portugal and Sweden

martinbean's avatar

So different countries will have different postal code formats. You’ll need to look up the formats for each of those countries and validate the postal code based on the country the user has selected.

Please or to participate in this conversation.