1

I have the following function in JavaScript, which takes in text for a USA phone number, tests against it to make sure nothing invalid is inputted, then formats it for display in the same text box.

It works well, but it needs to also accept phone extensions. The phone extension basically needs to pass through as is, so it can be any number of any characters, whether it begins with a space, a *, a ext, whatever the case is. I was asked not to worry about all of those cases, and as I said, to have the extension just pass through. I told my boss that we could just have another text box for the extension, but that isn't what he wants.

So, writing the regex for n number of any characters isn't difficult, the part I am more concerned about is how to grab these characters in the code that actually does the phone number formatting.

I'd guess I can just grab the characters after the valid phone number, but I'm honestly not sure how to do this, keeping in mind that the phone number is regex checked and the input may have had differing lengths. Any thoughts?

I would be grateful for any help! Here is the current code, which needs to be adjusted for accepting and returning the extension when it formats the phone number, keeping in mind the requirement that the extension can be any number of any characters:

function formatPhone(phoneObj) {
 var phoneStr = phoneObj.value,
  s2 = (""+phoneStr).replace(/\D/g, ''),
  m = s2.match(/^(\d{3})?[- ]??[\s]?(\d{3})?[\s]?(\d{4})$/);
  phoneObj.value = (!m) ? null : "(" + m[1] + ") " + m[2] + "-" + m[3];
}

2 Answers2

1

First of all you could convert you string to lower case or make you regular expression case insensitive so that you don't have to deal with case differences.

There might be other ways to parse phone numbers but with given regular expression you could make a change like this one:

^(\d{3})?[- ]??[\s]?(\d{3})?[\s]?(\d{4})(\s+(ext|\*)?\s*\d{4})?$

Here I assume that there's a space before and after ext or *. Hope this gives you ideas and you can improve it to match your requirements.

Also check these answers:

Community
  • 1
  • 1
Igor Nikolaev
  • 4,597
  • 1
  • 19
  • 19
  • Thanks for the input. This doesn't answer my question though. Please reread. As noted, I need to be able to let n number of any characters through for the extension, when the phone formatting function is completed. The linked questions also do not address this specific problem. – Stephanie Fischer Dec 25 '16 at 22:19
  • Well your question wasn't quite cleat about that, because you mentioned that extension has to start with `ext`, `*` or `space` so I thought you wanted some kind of validation of the format of extension part as well. Glad that you figured that out. – Igor Nikolaev Dec 27 '16 at 05:30
  • Nope, did not state that. I stated that it could start with anything. Again, reread. Thanks..... – Stephanie Fischer Dec 28 '16 at 23:31
0

I figured it out. I thought it was more difficult than it really was. Silly me. Solution:

function formatPhone(phoneObj){
  var phoneStr = phoneObj.value,
  s2 = (""+phoneStr).replace(/\D/g, ''),
  m = s2.match(/^(\d{3})?[- ]??[\s]?(\d{3})?[\s]?(\d{4})?(.*)?$/);
  phoneObj.value = (!m) ? null : "(" + m[1] + ") " + m[2] + "-" + m[3]+ "x" + m[4];
}