Problem With Regular Expressions?

kasirao
Kilo Contributor

Hello Team

I have created one field that is like this

In this field i want fill the data in following format.

Two Strings (TS);

Two Digits(12);

again Two Strings(ES);

Four Digits(1234);

Here I would like to save the field data based on my below requirement.

When   i   am fill this field exactly above format at that time only that field data will saved.

When ever i am fill this field wrong format at that time display the user friendly message to on that field (Message is like this "Invalid Format" please enter the valid format )

valid1.PNG

I am new to ServiceNow,please help me in this requirement.

4 REPLIES 4

ChrisBurks
Mega Sage

Hi Kataru,



Try this regex:   /^[A-Z]{2}\s[\d]{2}\s[A-Z]{2}\s[\d]{4}$/



That should check the pattern with the letter characters being strictly uppercase. If the case of the letters doesn't matter then you can use:



/^[A-z]{2}\s[\d]{2}\s[A-z]{2}\s[\d]{4}$/



If you are going to stuff the regex in a variable then you would do something like this:



var re = /^[A-Z]{2}\s[\d]{2}\s[A-Z]{2}\s[\d]{4}$/       \\notice no quotes


Or if using the RegExp object then like this:



var re = new RegExp("^[A-Z]{2}\\s[\\d]{2}\\s[A-Z]{2}\\s[\\d]{4}$")             \\notice the escaping of the back slash "\"



The breakdown is this:



^ = the beginning of the string


[A-z]{2} = in between the brackets limit this section to letters two times


\s = match a space


[\d]{2} = match a number digit two times and of course {4} would be four times


$ = end of the line:   so putting everything in between ^ and $ compares an exact match to the str



Example uses



var str1 = 'TS 12 EE 1234';


var str2 = 'TS12EE1234';


var str3 = ' TS d 123 1234';


var str4 = 'TS 12 EE 1234 PPsg';


var str5 = 'RP 44 NH 8765';


var re = /^[A-Z]{2}\s[\d]{2}\s[A-Z]{2}\s[\d]{4}$/ ;



str1.match(re); //finds match


str2.match(re); // fails to find match


str3.match(re); //fails to find match


str4.match(re); //fails to find match


str5.match(re); //finds a match



Note: remember the .match() method returns an array. If it doesn't find a match it returns null.



Here's a testing ground for javascript regular expressions: Scriptular - Javascript Regular Expression Editor


Hello chris


Here how to keep the field message can you please help me in this requirement.


My field name is Vehicle Number.


when i am field data like this " TS 12 EE 1234" no need get any field message.


When i am enter data like this "'TS 12 EE 1234 PPsg" i want get field message like this ("Please enter vehicle number in this format TS 12 EE 1234")


Please help me in this requirement


I believe pratyusha shows you how to do this in their post below.


Pratyusha
ServiceNow Employee
ServiceNow Employee

Hello Kasi Rao,



To fulfill your requirement, you would need to define a client script on the 'Vehicle Number' field. Here are the steps you should be doing -


1. Open the form and from the form context menu, go to Configure > Client Scripts.


2. Add a new client script with the below values -


      Type: onChange


      Field name: Vehicle Number


      Script:


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


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


      return;


  }



  // Use Regular Expressions to verify vehicle number


  var vehicleNumberRegex = /^[A-Z]{2} [0-9]{2} [A-Z]{2} [0-9]{4}$/;


  if (newValue.match(vehicleNumberRegex))


      return;


  else


g_form.showErrorBox('vehicle_number','Valid vehicle number format is TS 12 ES 1234',true); // Here vehicle_number is the column name of your coulmn labeled 'Vehicle Number'


}


3. Save this client script and try creating some sample records with this value for Vehicle Number.



Please let me know if you have any further queries.



Thanks,


Pratyusha