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
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

How to Set Integer Validation for multiple Variable at a Time. On change we can set condition only for one variable. Can you please help me with this.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Since Madrid, you can apply Regex Variable Validation. So no need to write and maintain Client Scripts.

Read about it in one of my previous articles:
Service Portal Catalog Items: Regex Field Validation [Madrid]

Number is even an out-of-the-box Regex Variable Validation.

So please first investigate applying this out-of-the-box mechanism before even looking at scripting this with Client Scripts.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

mr18
Tera Guru
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);
}