Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Validation for number for variable in Catalog item

mahfooz1
Tera Contributor

I have a text variable and I want the user should enter only numbers not text

1 ACCEPTED SOLUTION

mr18
Tera Guru

Create onChange client script select the particular variable and try below code

var value = g_form.getIntValue("<variable_name>");
if(isNaN(value)||isNaN(newValue)||(newValue < 0)){
g_form.showFieldMsg('<variable_name>','Please enter a numeric positive value!','error');
return;
}else{
g_form.clearMessages();
g_form.hideFieldMsg('<variable_name>',true);
}

View solution in original post

7 REPLIES 7

ShubhamGarg
Kilo Sage

Hi Mahfooz,

Use Out-of-box regular expressions to fulfill this criteria.

Go to your variable > "Type specifications" section > Select Validation Regex "Number" from drop-down list. > Save it.

 

In case, "Number" is not present in drop-down. Create new Validation Regex following this path: Service catalog > Variable Validation Regex > Create New.

Mark this response as correct/helpful if you find it helpful.

Regards,

Shubham

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Asbj_rn
Mega Expert

Try a catalog client script. 

extract the value "on change". 

is the isNaN native javascript function to validate. 

https://www.w3schools.com/jsref/jsref_isnan.asp

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

write onchange client script on that text variable

UI Type - ALL

Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var regexp = /^([0-9]+[0-9])/;
	
	if(!regexp.test(newValue))
		{
		alert('Only numbers are allowed');
		g_form.clearValue('variableName');
	}
	
	//Type appropriate comment here, and begin script below
	
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader