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

The phone number field does not automatically convert to the desired format but it can validate the incoming data.


dhasselquist
Mega Guru

Jon,



To match the possibility of having a plus sign and then a 0 and/or 1, I would use /\+0?1?/ (http://regexr.com/3frm1).



If I were validating this I would probably strip out any non-digit (\D), and then match the last 10 characters. This worked for the example you provided above, but I'm sure there are a number of edge cases that would throw it off. It seemed to me to be a happy medium.


Excellent Dylan.   Thanks a lot.   I've not done a lot with regex so this is extremely helpful.   Just couldn't figure it out.


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

Dr Chun,



Thanks so much.   I like this solution.   I really appreciate the response.   I've got all I need now so thanks to all who responded.   Excellent!!