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.

Check if some of the characters are numbers

user_ms
Tera Expert

Extract characters and check if the extracted value is a number.

 

If there is the following value, I want to extract the first 3 characters and determine whether the value is a number.
123-456

If there is no number, print an error message in the field.

 

Other conditions
Use Client Script
Check with onSubmit

 

Please let me know if you know.

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@user_ms Here is the script 

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var textBox= g_form.getValue('text_box');
	var pattern = /^\d{3}-/;
	if(pattern.test(textBox)){
		g_form.showFieldMsg('text_box','correct');
	}
	else{
		g_form.showFieldMsg('text_box','incorrect');
	}
	return false;
}

View solution in original post

5 REPLIES 5

Harish KM
Kilo Patron
Kilo Patron

HI @user_ms you can use the below script to check 1st 3 characters

 var sd = g_form.getValue('short_description');
  var strFirstThree = sd.substring(0,3);// check 1st 3 characters
  alert(strFirstThree);
  if(!isNaN(strFirstThree))
  {
    alert("number"); // if 3 char are numbers
  }
  else{
    alert("not a number"); // not number
  }
Regards
Harish

Sandeep Rajput
Tera Patron
Tera Patron

@user_ms Here is the script 

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var textBox= g_form.getValue('text_box');
	var pattern = /^\d{3}-/;
	if(pattern.test(textBox)){
		g_form.showFieldMsg('text_box','correct');
	}
	else{
		g_form.showFieldMsg('text_box','incorrect');
	}
	return false;
}

@user_ms Any further questions on this topic?

@Sandeep Rajput 

Sorry for the late confirmation.

I was able to do it that way.

I don't have any further questions on this topics.

Thank you for your help.