Lookup City by Zipcode for your WordPress form
Example JavaScript code and form elements code to lookup a city based on entered zipcode on your WordPress forms. You can change the region by setting the country as parameter if needed.
Last updated
Example JavaScript code and form elements code to lookup a city based on entered zipcode on your WordPress forms. You can change the region by setting the country as parameter if needed.
Last updated
Even though natively there is no build-in option in Super Forms to lookup a city based on an entered zipcode, you could achieve this relatively easily with some custom JavaScript code and using the Google Geocoding API.
For instance if you enter 7064BW as the zipcode, it will populate the field below that with the corresponding city name e.g. Silvolde.
What you will need are: A Text field named "enter_zipcode" a Text field named "city" and the API key from google. You can change the country name in the API request URL to lookup zipcodes from inside a specific region or country only.
In the example form code below, the city field is set to read only/disabled so that the user cannot alter it themselves. And a validation "Not empty" is applied to display an error in case the entered zipcode didn't match (populate) any city.
If you also wish to only allow your service for specific cities, you could hook into the form submission (PHP side), and return an error whenever the city field doesn't match the list of your cities. Please refer to an example on how to do this (you will need to adjust the code to your liking, but the use-case is the same):
Make sure to set the API key and country in the below code, and make sure this javascript code is loaded on the page where your form is displayed.
Example form elements code with only two Text fields (for entering a zipcode, and to populate a field with the corresponding city). You can copy paste this code under the [CODE] tab on the form builder page when creating a new form to test this out):