- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2024 08:53 AM
Hi,
I have a requirement where a single line text can allow between -1 and 1000000 only. Kindly help.
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2024 10:13 AM
Hi @Community Alums,
To enforce a validation rule for a single-line text field in ServiceNow to ensure the input is between -1 and 1,000,000, try below script:
Type: onChange
(function executeRule(current, previous /*null when async*/) {
var value = parseInt(g_form.getValue('your_field_name'), 10);
// Check if the value is a number and within the range -1 to 1000000
if (isNaN(value) || value < -1 || value > 1000000) {
g_form.showFieldMsg('your_field_name', 'Value must be between -1 and 1,000,000', 'error');
return false;
} else {
g_form.hideFieldMsg('your_field_name', 'error');
}
})(current, previous);
Thank you, please make helpful if you accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2024 10:13 AM
Hi @Community Alums,
To enforce a validation rule for a single-line text field in ServiceNow to ensure the input is between -1 and 1,000,000, try below script:
Type: onChange
(function executeRule(current, previous /*null when async*/) {
var value = parseInt(g_form.getValue('your_field_name'), 10);
// Check if the value is a number and within the range -1 to 1000000
if (isNaN(value) || value < -1 || value > 1000000) {
g_form.showFieldMsg('your_field_name', 'Value must be between -1 and 1,000,000', 'error');
return false;
} else {
g_form.hideFieldMsg('your_field_name', 'error');
}
})(current, previous);
Thank you, please make helpful if you accept the solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2024 10:55 AM
Hi there,
Regex Validation sounds the way to go for you:
- 2019-04-22 - Article - Service Portal Catalog Items: Regex Field Validation
- 2019-08-14 - Article - Regexes for Catalog Items Variable Validation, part 2
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2024 05:26 AM
Hi @Yashsvi ,
Without your answer, I wouldn't have done it. Bu I made it even easier.
var numb = g_form.getIntValue('retention');
alert(numb);
alert(typeof(numb));
if(numb < -1 || numb > 31556952000 ){
g_form.addErrorMessage("Retention should be between -1 and 31556952000 ");
g_form.setValue('retention', '');
}
Regards
Suman P.