Skip to main content
All CollectionsAdvanced Topics
Can I limit the zip code field to 5 digits?
Can I limit the zip code field to 5 digits?
Michael Fatica avatar
Written by Michael Fatica
Updated over a year ago

MetaLocator's default location search option, typically labelled "zip code" can actually take any user input that can be interpreted as a location.

This means users can search for

  • 53211

  • Milwaukee

  • "City hall"

  • 12345 N My Home Address, Denver, CO 80205

  • McDonalds on North Ave.

Sometimes, users in the United States would prefer that a full 5-digit zip code be required as input.

This requirement isn't viable when supporting those queries listed above, or when international or alphanumeric postal codes might be involved.

To support only 5-digit postal codes, the solution is to add a bit of JavaScript under Advanced > Custom Head Tag to the Interface which performs the validation.

<script>

jqLocator(document).ready(function(){

jqLocator('#ml_searchform .locator_submit:input[type="submit"]').on('click',function(e){

var ml_language = window.ml_language || {};
var isValid = false;
var userInput = jqLocator('#postal_code').val();

if(typeof(userInput) === 'string' && userInput.match(/^\d{5}$/)){
isValid = true;
}

if(!isValid){
swal(ml_language.LOCATOR_ERROR,"Please enter a 5-digit US zip code",'error');
}

return isValid;
});
});

</script>
Did this answer your question?