- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 12:25 AM
I have created a variable type as single line text but i want that it should only accept numeric values and it should not allow number greater than 10 , how to achieve this with the help of client script
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 12:51 AM
Hi,
Did you try using Validation Regex? you can do this without scripting.
> From your left navigator, open 'Variable validation regex'
> Create a new record with regular expression as \b([0-9]|10)\b
> Give required message.
> Save.
Now open your variable in catalog item
> In Type Specification tab, in validation regex field, select the regex which is created in above steps.
>Done.
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 12:34 AM
Hi
Validate with a RegEX using OnChange client script and display an info message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 12:36 AM
Hi,
so what did you start with and where are you stuck?
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 02:06 AM
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var num=newValue;
if(isNaN(num)){
g_form.addInfoMessage('Numbers is acceptable as hours');
g_form.clearValue('<weekly_total_working_hours>');
return;
}
else if(parseInt(num) >=1 || parseInt(num) <= 45);
alert('Numbers from 1 to 45 are allowed');
//g_form.clearValue('<weekly_total_working_hours>');
g_form.setValue('weekly_total_working_hours','');
}
I have written this script which is not working as expected .

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 02:35 AM
Hi,
Did you try the regex i mentioned?
Or else you can update your script as below and try
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if(isNaN(parseInt(newValue)){
g_form.addInfoMessage('Numbers is acceptable as hours');
g_form.clearValue('weekly_total_working_hours');
}
else if((parseInt(newValue) <1) || (parseInt(newValue) > 45))
{
alert('Numbers from 1 to 45 are allowed');
g_form.clearValue('<weekly_total_working_hours>');
}
}
Regards,
Sumanth