logo

NJP

Adding Zip Code Fields on a form

Import · Jun 08, 2022 · article
  • If You are adding a US Zip Code Field to a form, you can build it as a string field from configure form design and restrict the Maximum length to 5.
  • You can then add an onChange Client Script on the field to validate the entries and add this code:
    function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } var newVal = newValue.toString().length; if (newVal != 5) { g_form.showFieldMsg('u_zip_code', 'Please add a 5 digit Zip Code', 'error'); } var re = /^[0-9]*$/; if (!re.test(newValue)) { g_form.showFieldMsg('u_zip_code', 'Please add a valid zip code', 'error'); return false; } return true; }​
    • The code verifies for you if the Zip Code is indeed 5 digits and since the field is a String, it also makes sure you don’t include alphabets in the field.
    • Note: Replace u_zip_code with the name of your field and this will not work for zip codes in the format of 12345-1234.

image

View original source

https://www.servicenow.com/community/developer-articles/adding-zip-code-fields-on-a-form/ta-p/2297945