Number only with number limit to 10 digits for variable

servicenowsnow
Mega Expert

I'm working on one of catalog development , one variable restricted for only numbers ( we need to update only numbers ...if we give letters it should give error). and the number limit to 10 digits only.

Please help me on code needed for onchange client script.

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Something like this?

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

	if (isLoading || newValue === '') {
		return;
	}

	g_form.hideFieldMsg('your_variable', true);
	
	var val = newValue;
	var msg = getMessage('some message');

	if((isNaN(val) == true) || (val.length > 10)) {
		g_form.showFieldMsg('your_variable', msg, 'error');
	}

}

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

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

View solution in original post

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Something like this?

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

	if (isLoading || newValue === '') {
		return;
	}

	g_form.hideFieldMsg('your_variable', true);
	
	var val = newValue;
	var msg = getMessage('some message');

	if((isNaN(val) == true) || (val.length > 10)) {
		g_form.showFieldMsg('your_variable', msg, 'error');
	}

}

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

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Harsh Vardhan
Giga Patron

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

 

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

 

          return;   }

    //Type appropriate comment here, and begin script below

  var age=g_form.getValue('variable name');

  if (isNaN(age) )

    {

  alert("value should be in number");

  g_form.setValue('Variable name','');

  return false;

    }

if(age.length!== 10){


alert("number should be 10 digits");


return false;


}

 

}

Mark Roethof
Tera Patron
Tera Patron

Hi there,

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

Please mark this answer as correct if it solves your problem. This will help others who are looking for a similar solution.
Thanks.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn