
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2022 11:26 PM
Hi,
I have an onChange Client Script ensuring that minimum eight characters are entered in a string field.
It looks like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var myFieldValue = g_form.getValue('u_account_number');
if (myFieldValue.toString().length < 8 ) {
g_form.setValue('u_account_number','');
alert('Please enter 8 or more characters.');}
}
Now I want to expand that so that only numbers from 0 to 9 should be possible. How can I achieve that?
Best regards
Thomas
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 12:16 AM
Hi Thomas
Did you try the regex option suggested by Jerick, that works.
or you can also do this too which is same as Validation regex but through script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regexp = /^[+]?\d*$/;
var myFieldValue = g_form.getValue('u_account_number');
if (regexp.test(newValue)) {
if (myFieldValue.toString().length < 😎 {
g_form.setValue('u_account_number', '');
alert('Please enter 8 or more characters.');
}
} else {
g_form.setValue('u_account_number', '');
alert('Please enter Numbers');
}
}
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2022 11:36 PM
Hi @Thomas G,
If this field is a catalog variable there is an existing regex validation see screenshot below for reference.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2022 11:41 PM
Hi Jerick,
Thanks for your reply. I should have mentioned that it is a string field on the incident table so alas no regex type as an option.
Best regards
Thomas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2022 11:44 PM
Hi @Thomas G
You want to valiate the length and numbers? Can you explain in detail of your requirement with few examples.
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2022 11:49 PM
Hi Rohila,
Yes; I want to validate both minimum length and make sure that only numbers are allowed. It should only be possible to enter numbers between 0 and 9 but the number entered should be minimum eight numbers long. Eg.: 20028929
Best regards
Thomas