- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2020 10:51 AM
I have a decimal field on which i have to apply validation that it shows only one digit after the decimal point .
And also applying validation on String field that it shows only two characters.
How can I apply validation in servicenow?
Thank you for any help.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2020 11:09 AM
Hi Sanel,
You can create 2 onChange client scripts that runs onChange() of decimal field & string field as below.
1. For decimal check.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = g_form.getValue('new'); //replace decimal field name here
var newis=parseFloat(gr).toFixed(1); //converts 12.3456 to 12.3
alert('Decimal field can have only 1 number after decimal');
g_form.setValue('new',newis); //replace new with decimal field name
}
2. For String field check.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = g_form.getValue('new'); //replace new with string field name
if(gr.length>2 || gr.length<2)
{
alert('String field can have only 2 characters maximum');
g_form.setValue('new',''); //replace new with string field name
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2020 10:57 AM
Check below
https://www.servicenowguru.com/system-definition/controlling-decimal-field-places/
Regards,
sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2020 11:09 AM
Hi Sanel,
You can create 2 onChange client scripts that runs onChange() of decimal field & string field as below.
1. For decimal check.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = g_form.getValue('new'); //replace decimal field name here
var newis=parseFloat(gr).toFixed(1); //converts 12.3456 to 12.3
alert('Decimal field can have only 1 number after decimal');
g_form.setValue('new',newis); //replace new with decimal field name
}
2. For String field check.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = g_form.getValue('new'); //replace new with string field name
if(gr.length>2 || gr.length<2)
{
alert('String field can have only 2 characters maximum');
g_form.setValue('new',''); //replace new with string field name
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2020 11:20 AM
Hi Sanel,
Here's an article on the recommended practice to validate input data using Variable Validation with RegEx:
https://community.servicenow.com/community?id=community_blog&sys_id=ed0733a2dbe490104819fb24399619dc
I'm not sure if you want this for the Service Catalogue or for forms in the classic UI; there might be some differences in how to apply this, but in both cases I'd use RegEx and would look something like:
^[0-9]*(\.[0-9])?$
I drafted that TOOO fast and will probably have flaws, but you can check it with in following page:
To make sure that it works when there's no decimal, when there is, not allowing two decimal positions, etc, etc...
Hope this helped. Kind regards,
Miguel A. Garcia
SN Technical Consultant at FlyForm Ltd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-08-2020 11:22 AM
PS: you can also check the RegEx library for already made expressions to reuse: