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.

how to set numeric value for a variable in service catalog like mobile number...

bvk
Kilo Contributor

how to set numeric value for a variable in service catalog like mobile number...

1 ACCEPTED SOLUTION

You can use this an onchange client script to only accept numeric. If user is trying to write non numeric value then error occurs: Please enter numeric values only in the phone number field



function onChange(control, oldValue, newValue, isLoading) {  


    if (!isLoading) {  


          if(newValue != '') {  


                //check to see if there are any non numeric characters in the string  


                if(isNaN(newValue) == true) {  


                      alert("Please enter numeric values only in the phone number field");  


                      g_form.setValue('phone_number', '');  


    }  


  }  


}  


}  



And you can set max lenght to 10 from variable's default value field -> Variable attributes : max_lenght=10



Capture.PNG



Thanks,


Joonas



please like or mark correct based on the impact of response


View solution in original post

11 REPLIES 11

Aakash Shah4
Tera Guru

Hi Varun,



If you are looking for the users to enter only numeric values then have a look at this.



Numeric Characters only - Service Catalog Item


Also this is an alternate solution



Can i force a from field to only accept numeric value?


Siddartha Gudim
Tera Guru

We dont have a variable of type integer.



so Use a string type field in conjunction with a catalog client script to limit this field to numbers.   Replace <your variable> below:



Type:   OnChange Catalog Client Script


function onChange(control, oldValue, newValue, isLoading) {


    if (!isLoading) {


          if (newValue != '') {


                //get the integer value of the field


                var nbrwn = g_form.getIntValue('<your variable>');


                //check to see if there are any non numeric characters in the string


                if ((isNaN(newValue) == true) || (nbrwn < 0)) {


                      alert("This field should contain a whole number 0, 1, 2, 3, ... (and so on)");


                      g_form.setValue('<your variable>', '');




                }




          }


    }


}



please like or mark correct based on the impact of response


Sampath8
Giga Expert

Hi Varun,



You can try the below script.


OnChange: Variable Name.



Script:



function onChange(control, oldValue, newValue, isLoading) {


if(isLoading || newValue == ''){


return;


}


var pattern = /^[0-9]*$/;


if(!pattern.test(newValue))


{


alert('please enter only digits');


g_form.setValue('variable_name', ''); // adjust field name accordingly if you want to blank it out.


}


}




Regards,


Sampath.