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.

onChange Client Script for maximum number of digits

Bruce11
Giga Expert

Hi,

I have an onChange client script that is not working the way it should. I am trying to notify users that the maximum numbers allowed is 10 digits (if they are trying to enter more than 10). I also want the variable to allow only integer. Below is what i have but its not working properly.

 

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

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

if (isNaN(parsed)){

parsed = "";

}

if (parsed != newValue) {

alert("Please enter a valid integer number ");

g_form.setValue("variable_name", parsed);

//if more than 10
if(parseInt(newValue) > 10){
alert(getMessage("10 digits max"));
g_form.setValue("variable_name", '');
return false;
}
}

}

 

Thank you.

1 ACCEPTED SOLUTION

Amar Kutlaria
Mega Guru

You can try onchange script provided by Kunal or

You can use variable attributes to set the max_length on the variable to stop user from entering more than 10 characters. Then, you use on change client script to validate entered characters are integers only.

 

find_real_file.png

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

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

  return;

  }

  var regexp = /^([0-9]+[0-9])/;

  if(!regexp.test(newValue)) {

  alert('Please enter a 10 digit number');

  g_form.setValue('variable name',''); // set variable to empty

  }

}

 

If I answered your question, please mark Correct/Helpful.

View solution in original post

5 REPLIES 5

Omkar Mone
Mega Sage

Hi 

Only for characters it would go something like this - 

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

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

  return;

  }

  var regexp = /^[a-zA-Z]+$/;

  if(!regexp.test(newValue)) {

  alert('Please enter only characters');

  g_form.setValue('variable name',''); // set variable to empty

  }

}

 

Mark correct answer and close this thread if it helps.

 

Regards,

Omkar Mone