Need to limit to 10 digit number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2022 07:01 PM
I need to limit the field to 10 digit number. Here is my code. need assistance.
Thanks.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2022 07:10 PM
Hi,
You can simply use:
if (newValue != '') {
var nbrwn = g_form.getValue('npi_no');
if (nbrwn.toString().length > 10) {
g_form.clearValue('npi_no');
alert(getMessage("Please enter a valid NPI No.");
}
}
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2022 07:14 PM
Hi
you have just a little typo and I also recommend removing any white spaces at the beginning and at the end of the user input.
Line 3: if (!regex.test(newValue) || newValue.length != 10)
Kind regards
Maik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2022 09:18 PM
Johnson,
Fixed the script. I've changed the regex to also check if it's 10 digits or not.
function onChange(control, oldValue, newValue, isLoading) {
if (newValue != '') {
try {
var nbrwn = g_form.getIntValue('npi_no');
var regex = /^\d{10}$/;
if (!regex.test(newValue)) {
alert(getMessage("Please enter a valid NPI No."));
g_form.clearValue('npi_no');
}
} catch (e) {
alert(e.message);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2022 09:43 PM
Hi,
you can simply use Variable validation Regex and then link it with that variable
Create New record
Regex - ^[0-9]{10}$
Then link it with your variable
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader