Single line to have values between certain range

Community Alums
Not applicable

Hi,

I have a requirement where a single line text can allow between -1 and 1000000 only. Kindly help.

Regards

Suman P.

1 ACCEPTED SOLUTION

Yashsvi
Kilo Sage

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. 

View solution in original post

3 REPLIES 3

Yashsvi
Kilo Sage

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 Roethof
Tera Patron
Tera Patron

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

LinkedIn

Community Alums
Not applicable

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.