I have a requirement for validation of zip code format ##### (allow only up to 5 digits and always 5

Trupti Krishnam
Tera Contributor

var regex = /[0-9]{5}(?:-[0-9]{4})?$/;
if (!regex.test(newValue)) {
g_form.showFieldMsg('property_zip_code', 'Please enter a valid zip code #####', 'error');
return false;
}
return true;
}

Its working but if i give more than 5 digits its taking can anyone help me with this !!! 

1 ACCEPTED SOLUTION

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Trupti Krishnam ,

Try the modified regex:

 

if (!/^[0-9]{5}$/.test(newValue)){

g_form.showFieldMsg('property_zip_code', 'Please enter a valid zip code #####', 'error');
return false;

}

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

4 REPLIES 4

Appli
Mega Sage
Mega Sage

Hi

 

Please try to use:

var regex = /^[0-9]{5}$/;

 

Hope it helps

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Trupti Krishnam ,

Try the modified regex:

 

if (!/^[0-9]{5}$/.test(newValue)){

g_form.showFieldMsg('property_zip_code', 'Please enter a valid zip code #####', 'error');
return false;

}

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Trupti Krishnam
Tera Contributor

Thank you 🙂

Ashwini Jadhao
Giga Guru

Hi Trupti,

Please try below Regex

/^\d{5}(?:-[0-9]{4})?$/