The CreatorCon Call for Content is officially open! Get started here.

Regex Assistance Needed

jonmulherin
Giga Expert

I need some help converting various phone number formats, all US-based at this point, into simple 10 digit format.   I'm dealing with all kinds of variations in the phone numbers and can't get the regex string correct to convert whatever value is input down to 10 digits.   Some of the things I'm dealing with in the mobile phone field are as follows:

(315) 506-3302

724.256.1695

+18566255328

+01 (414) 331-2475

+2242507689

1-503-789-7330

01 714 566 5532

+01 806-438-4059   +01 806-577-1741   (this guy has two listed)

513/335-0705

Direct Connect 168*9674*120

14143699310

NONE

N/A

One of my teammates developed a form that allows people to opt in or opt out of receiving an SMS notification when one of our (non-ServiceNow) systems go down.   He is taking whatever is in the sys_user mobile_phone field and pre-populating the form with it.   No validation is done on the form.   Once the user submits, a record is written to the ticket table.   I've since created a business rule to create or update the SMS device for the user and then subscribe the user to the notification, or to validate it's active if it already exists.   The issue I'm having is the SMS service providers expect just 10 digits.   They don't want the +1, +01 or any of the other stuff seen above.   I think if I can get some assistance in how to remove the leading +1, +, +01 I can get the rest of it, but nothing I've tried thus far is removing the +.   Those above beginning with just a 1 or 01 with no + might pose an issue as well.   I know we've got a lot of experts out here and I could sure use the help with this one.

Thanks a lot,

Jon

1 ACCEPTED SOLUTION

drjohnchun
Tera Guru

I was thinking along the same line as Dylan and here's working code, with some added cases for better testing:



var input = ['(315) 506-3302',
  '724.256.1695',
  '18566255328',
  '+01 (414) 331-2475',
  '2242507689',
  '1-503-789-7330',
  '01 714 566 5532',
  '+01 806-438-4059   +01 806-577-1741   (this guy has two listed)',
  '513/335-0705',
  'Direct Connect 168*9674*120 ',
  '14143699310',
  'NONE',
  'N/A',
  '12345',
  'Off:   +1 (914) 323-5775',
  'Cell: +1 (214) 649-0738'
];



for (var i = 0; i < input.length; i++) {


  // remove all non-numbers and keep only last 10 characters
  var clean = input[i].replace(/\D/g, '').slice(-10);


  // check if all 10 characters are numbers
  gs.info(clean + ' is ' + (/\d{10}/.test(clean) ? '' : 'in') + 'valid phone number from ' + input[i]);


}



and the output from Background Script:



3155063302 is valid phone number from (315) 506-3302
7242561695 is valid phone number from 724.256.1695
8566255328 is valid phone number from 18566255328
4143312475 is valid phone number from +01 (414) 331-2475
2242507689 is valid phone number from 2242507689
5037897330 is valid phone number from 1-503-789-7330
7145665532 is valid phone number from 01 714 566 5532
8065771741 is valid phone number from +01 806-438-4059   +01 806-577-1741   (this guy has two listed)
5133350705 is valid phone number from 513/335-0705
1689674120 is valid phone number from Direct Connect 168*9674*120
4143699310 is valid phone number from 14143699310
is invalid phone number from NONE
is invalid phone number from N/A
12345 is invalid phone number from 12345
9143235775 is valid phone number from Off:   +1 (914) 323-5775
2146490738 is valid phone number from Cell: +1 (214) 649-0738



When there are two numbers entered, this will only keep the last one. Here, "valid" means the last 10 characters are all numbers. You could write a complex set of regex for all cases, but this simple approach may be more robust as long as it meets your needs.



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

View solution in original post

9 REPLIES 9

venkatiyer1
Giga Guru

Hi Jon,



Before getting into the regex part I was wondering whether we could fix this issue in a different way. If the data type of your field is phone_number_e164 (which is available in ServiceNow with Dublin Release- Using Phone Number Fields - ServiceNow Wiki ) which you can choose from the dictionary field   it allows you to choose a location which when specified for e.g. North America irrespective of what you type it would format automatically as +1 (XXX) XXX -XXXX.




find_real_file.png


Very interesting. Thank you. We're going to look into this but I think we may run into some issues. The Phone Number (E164) field type is not available for catalog items / record producers, which is what the users will use to fill out their information. The field is pre-populated from the user record, which is populated via LDAP from AD.



So, if we were to set the phone territory on each location and changed the field type on mobile_phone, the next time LDAP runs would all phone numbers be formatted correctly, or would they all still be what is pushed from AD with a message stating they are invalid displayed?



Let's say we don't touch the mobile_phone field on sys_user but instead change the field type on the ticket table to E164. When the record producer prepopulates the text field it will do so with the information from AD. Most will be in the wrong format, even if the user types in the number. When the user submits, will the fact the u_mobile_phone field in the ticket table is E164 properly convert the number when inserting the record?





Jon



Jon Mulherin


EA Process Architect


Off: +1 (914) 323-5775


Cell: +1 (214) 649-0738


Venkat,



Thank you.   I used your suggestion and converted the phone number field in the ticket table to E164 and wrote a catalog client script to force the user's input on the form to be just ten digits.   Now when the record is written to the ticket table it preserves the country code, which may be needed by some SMS service providers once we move this to EMEA and APAC.   Now I know the format will always be the same based on the phone territory so this was a huge help.   I've marked your solution as helpful



Thanks so much,


Jon


venkatiyer1
Giga Guru

The Phonenumber field (http://wiki.servicenow.com/index.php?title=Using_Phone_Number_Fields#Require_Territory_Format_Valida...) helps in validating incoming data. (see this article for checking validity https://community.servicenow.com/thread/183583)




Option 1 we store all the valid phones under the country and use the other option to store other numbers. We can the option 1 in case we are pulling the data from AD. For user entering through Record Producers we can limit the characters by having a regex matching the format of (XXX) XXX XXXX on the input box field using the field dictionary attributes defined in the link below https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/administer/field-administration...