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

I used the exact same code you offered. I didn't see anywhere in there that necessarily required me to alter it.


Is your variable named "number_variable"?


Not 15 minutes after I said that, I realized my error. Thanks for your help!


This is very helpful!

Can you add a script that only allows 0-100 or max 100?

 

Thank you!

kevinray
Giga Expert

Hi Leah,


I might have something better for you. The proposed solution works, but it still let's you type alpha characters and it isn't until you leave the cell that you are warned. I personally want my number fields to literally only let them enter numbers. No warnings when leaving the field. The field literally will not let they type any alpha characters.



You can do this by creating an OnLOAD client script.



- Create OnLOAD Client Script


- Change "VARIABLE_NAME_GOES_HERE" to proper variable name


- SAVE



function onLoad() {


  //restrict character inputs to numbers


  try{


  var fieldInput = g_form.getControl('VARIABLE_NAME_GOES_HERE');


  fieldInput.onkeypress = fieldInput.onpaste = checkNumberField;


  }


  catch(err){


  //unable to limit character input


  }


}




function checkNumberField(mc) {


  var e = mc || event;


  var inputChr = e.type == 'keypress'


  ? String.fromCharCode(e.keyCode || e.which)


  : (e.clipboardData || window.clipboardData).getData('Text');


  if (/[^\d]/gi.test(inputChr)) {


  return false;


  }


}