
- 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:55 PM
- 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-29-2022 12:29 AM
Hi Rohila,
I tried that. It gives me a parsing error (Unexpected token) in line 9. The line with g_form.setValue('u_account_number', '');
Best regards
Thomas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 12:33 AM
Hi
Try this.
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.clearValue('u_account_number');
alert('Please enter 8 or more characters.');
}
} else {
g_form.clearValue('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-29-2022 12:40 AM
Hi again,
By adding the number 8 after < in the line:
if (myFieldValue.toString().length <
This one seems to work. Thank you! 🙂
Best regards
Thomas