Allow zip code only from the US

Hello, I want to allow only US zip codes, I’ve done some research but I have found nothing so far. Can someone guide me on how I can do this, please?

Hi Pedro. Do you have a list of valid ZIP codes? Or is there a pattern for a valid US ZIP code that you can apply? If so, you can use the gform_field_validation filter to validate against your list:

Hello @chrishajer. No, I don’t have a list to valid against, I just wanted to validate zip code only in the US with no other country allowed, and also I wanted it to support the nnnnn and nnnnn-nnnn patterns.

Thank you I have already verified this documentation, pasting all US zip codes to validate would be time-consuming and overwhelming.

While I was doing some research I saw something about regex that could allow me to do this, I came upon this combination for all US zip code:
/(^\d{5}$)|(^\d{5}-\d{4}$)/ or postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;

But I don’t know how to implement it because I don’t have too much programming knowledge. Please, any further directive would be welcome.

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