Brian Kimani
Tera Contributor
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-08-2022 10:16 AM
- 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.
- 1,440 Views
Comments
_ChrisHelming
Tera Guru
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
06-09-2022
06:16 AM
the following would work for both zip code "types", regular 5 digit and "plus four."
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (!/^\d{5}(?:-\d{4})?$/.test(newValue)) {
g_form.showFieldMsg('u_zip_code', 'Please enter a valid zip code', 'error');
return false;
}
g_form.hideFieldMsg('u_zip_code', true);
return true;
}
or you could just use an input validation on the field with the regex: /^\d{5}(?:-\d{4})?$/