- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 03:33 AM
Hello All,
I am trying to restrict single line text variable limit to 40 numbers with the help of RegEX (^[1-4]?[0]$|^[1-3]?[0-9]$)
like field will not accept any character and it will receive only digit minimum 1 hours and maximum 40 hours.
Solved! Go to Solution.
- Labels:
-
Incident Management
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 03:59 AM
Hi,
Correct me if I am wrong.
What you need is Variable value should between 1-40 not 40 lengths value.
If yes then have onChange client script on that variable.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var val=g_form.getValue('variable name'); //enter correct variable name
if(val<0 && val>40){
g_form.setValue('variable name',''); //enter correct variable name
}
}
Thanks,
Dhananjay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 04:03 AM
Hi Radhe,
Are you saying it should only accept numbers from 1 to 40 and no other string?
if yes then try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if(isNaN(newValue)){
alert('Please enter only numbers');
g_form.clearValue('fieldName');
}
else if(parseInt(newValue) <1 || parseInt(newValue) > 40){
alert('Only numbers from 1 to 40 are allowed');
g_form.clearValue('fieldName');
}
}
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-19-2020 06:18 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('<field name>');
return;
}
else(parseInt(num) >=1 || parseInt(num) <= 40){
alert('Numbers from 1 to 40 are allowed');
g_form.clearValue('<Field name>');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 03:52 AM
Hello Radhe,
Please refer below link to get more information about single line text variable validation.
ensuring a single line text field only has numbers
Allow only numeric data in a single line field
Regards,
Harshal.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 03:59 AM
Hi,
Correct me if I am wrong.
What you need is Variable value should between 1-40 not 40 lengths value.
If yes then have onChange client script on that variable.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var val=g_form.getValue('variable name'); //enter correct variable name
if(val<0 && val>40){
g_form.setValue('variable name',''); //enter correct variable name
}
}
Thanks,
Dhananjay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 04:30 AM
Also if you want regEx then below is the script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val=g_form.getValue('account_no'); //enter correct variable name
var pattern= /\d{2}/;
if(val.search(pattern)==-1)
{
alert("Please enter correct number");
g_form.setValue('account_no',''); //enter correct variable name
}
else{
// do nothing
}
}