allow only integer in a variable

leahp
Kilo Guru

I have a wizard that has several variables that should only allow the user to enter numeric values.  

How I do enforce that in a wizard client script?

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

It depends on how you want to implement it exactly, but an onChange Catalog Client Script on the variable should do the trick:



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


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


      return;


  }



  //verify what was entered by using ServiceNow method that returns numeric characters only


  var parsed = g_form.getIntValue("number_variable");


  //if "Not a Number", clear the parsed value


  if (isNaN(parsed)){


      parsed = "";


  }



  //if the input and parsed values are different, set the variable value to the parsed value


  if (parsed != newValue) {


      alert("Please enter a valid integer number - stripping out non-numeric characters");


      g_form.setValue("number_variable", parsed);


  }


}



The above example will verify what was entered and if any non-numeric characters were input, will strip them out of the variable and pop-up an alert message.   Another way to do it would be just display an error message under the field without modifying it and then have an onSubmit script that would do the final check on all your variables to make sure they are integers.   Again, it all depends on how you want the user experience to be.



You could also inject some JavaScript into the input fields that only allows numbers to be entered, but that might be a little risky.



It's surprising there is still no OOB number variables for the catalog.


View solution in original post

13 REPLIES 13

Jim Coyne
Kilo Patron

It depends on how you want to implement it exactly, but an onChange Catalog Client Script on the variable should do the trick:



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


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


      return;


  }



  //verify what was entered by using ServiceNow method that returns numeric characters only


  var parsed = g_form.getIntValue("number_variable");


  //if "Not a Number", clear the parsed value


  if (isNaN(parsed)){


      parsed = "";


  }



  //if the input and parsed values are different, set the variable value to the parsed value


  if (parsed != newValue) {


      alert("Please enter a valid integer number - stripping out non-numeric characters");


      g_form.setValue("number_variable", parsed);


  }


}



The above example will verify what was entered and if any non-numeric characters were input, will strip them out of the variable and pop-up an alert message.   Another way to do it would be just display an error message under the field without modifying it and then have an onSubmit script that would do the final check on all your variables to make sure they are integers.   Again, it all depends on how you want the user experience to be.



You could also inject some JavaScript into the input fields that only allows numbers to be entered, but that might be a little risky.



It's surprising there is still no OOB number variables for the catalog.


Thanks, Jim - that worked like a champ!



But it would be nice if there was a true integer variable type


Hi Jim, this looks like something I am looking for but the non-numeric characters are not being stripped out of the variable and the alert message displays regardless. Is there something that needs to be adjusted in here that I am not quite seeing?



Thank you,


Heather Swearingen


Can you paste your code here so we can have a look?