0

I am working on a regular expression for Canadian phone number in javascript and / or in jQuery. I'm having some trouble into the formating after I have passed my main regular expression.

Mainly, I need to format a phone number in this way when the user leave the input field :

  1. 111-222-3333
  2. 111-222-3333 #44444 (up to 5 digits)
  3. 1-222-333-4444
  4. 1-222-333-4444 #55555 (up to 5 digits)

As you can see above, I want to be able to format a normal phone number and a toll free number at the same time.

The code

In my HTML, I have done a simple input field.

<input id="assure_telephone" placeholder="Phone number" name="assure_telephone" maxlength="25" type="text" />

For my jQuery, I picked up the code found in my large file and simplified it a little bit. We need to focus on my regular expressions.

$('#assure_telephone').bind('change', function(){
    // Delete all caracters and specials caraters except numbers
    telephone_user = $('#assure_telephone').val().replace(/[^0-9]/g, '');

    // Format the new phone number
    telephone_user_regex = telephone_user.replace(/(\d{3})(\d{3})(\d{4})(\d{0,5})/, "$1-$2-$3 #$4");

    $('#assure_telephone').val(telephone_user_regex);
});

The logic behind my code

As you can see, I'm starting by removing all special caracters to only keep numbers and then after I apply a formating with the .replace() Javscript function.

Link to my actual demo : http://jsfiddle.net/y201gcdg/6/

As you can see, it is pretty obvious that a toll free won't work as my formating is really made for normal phone number and not toll free.

My question is : Is there any way to work arround with the length of my telephone_user_regex variable to detect if it is a toll free or no OR is there any way to acheive it with a better regular expression?

Alternatively, I founded this on Stackoverflow that might be helping : https://code.google.com/p/libphonenumber/ (Source: Phone number format in Javascript)

Other ressource : http://www.w3schools.com/jsref/jsref_replace.asp

EDIT#1 - Deleted it after an answer that was not usefull.

EDIT#2 - Possible answer : Count the caracters Source : A comprehensive regex for phone number validation

I could start with my first replace() function and then count the number of caracter, if it exceed X number, then apply a formating, else do an other one.


EDIT#3 - As I did not wanted to make an answer for my own question, I will post my workarround here.

var typephone = type;

// We take out all caracters except 0 to 9
var telephone_user = $('#'+typephone).val().replace(/[^0-9]/g, '');

// Now I can make switch case to detect the kind of phone number I need to format
switch(telephone_user.length) {
           case 0 :
           case 1 :
           case 2 :
           case 3 :
           case 4 :
           case 5 :
           case 6 :
           case 7 :
           case 8 :
           case 9 :
                console.log('Your phone number is too small');
                break;
           case 10 : 
                console.log('This is a phone number 111-222-3333');
                break;
 }

In this way, I am able to detect the length and I could be able to know if it is a toll-free or a normal phone number. I might have trouble when it will come to the point the customer wrote something like this : 111-222-3333 #44444. After the special caracter removal, I won't be able to know if it was an extension.

Community
  • 1
  • 1
Patrice Poliquin
  • 373
  • 10
  • 21
  • Why can't you validate as they type each character ? A phone number is simple for regex. You just have to decide what is allowed. –  Nov 03 '14 at 20:01
  • In fact I could validate as they type, but I think it will resolve as the same... The ultimate goal is to properly format the phone number with a toll-free and an extension as conditions. – Patrice Poliquin Nov 03 '14 at 20:06
  • [The mistake you're making is trying to pattern match the raw phone number. Strip all non-digit characters first, and format the remainder based on length](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation). – zzzzBov Nov 03 '14 at 20:06
  • I would use a separate field for each major part of the phone number. Area code, Number, Extension. –  Nov 03 '14 at 20:07
  • @sln, please don't ever do that. It's awful usability. Use an `` element, and allow devices to provide the appropriate means of inputting a telephone number. – zzzzBov Nov 03 '14 at 20:08
  • @sln I do not want to make this kind of thing. As zzzzBov said, it is not usefull to manually have to write down ALL the information. The length() could be a good work arround. Thx for the link. – Patrice Poliquin Nov 03 '14 at 20:11
  • @zzzzBov - And your solution is to count the numbers in the string !? –  Nov 03 '14 at 20:11
  • I would have borderless separate fields that auto-tab to next when filled in correctly. –  Nov 03 '14 at 20:14
  • As I am only working with Canadian phone number, I think I can come to a working solution buy counting them. The goal is to make the formating automatic for the customer. – Patrice Poliquin Nov 03 '14 at 20:15
  • @sln, why do you care if a user writes `234-567-8901` vs `(234) 567-8901` vs `2345678901`? Worse, yet, with your method how do I write a number like `011-44-7981-897555`? – zzzzBov Nov 03 '14 at 20:15
  • So if the user enters 6 numbers, what do you do then ? –  Nov 03 '14 at 20:16
  • I can force an error that I require at least 10 digits after I removed all the special caracters. – Patrice Poliquin Nov 03 '14 at 20:17
  • @sln, you validate the field and let them keep typing. – zzzzBov Nov 03 '14 at 20:17
  • I think its sloppy when a user is allowed to enter invalid characters in a specific field type. Maybe he enters `.1j3jp60(123976907`, that tells me his intent is not a phone number. It's just sloppy. –  Nov 03 '14 at 20:22
  • Well, replace(/[^0-9]/g, ''); this kind of function removes all caracters that are not 0 to 9. So we don't really care and the user experience is still good since every one have there own way to write down phone number. – Patrice Poliquin Nov 03 '14 at 20:23

1 Answers1

0

Here's my suggestion:

  1. Don't replace # (i.e. do replace(/[^0-9#]/g, '')), because otherwise you couldn't detect if the 11th character is the country code or an extension.
  2. You can then use the position of the # to determine the length of the actual number (without extension), i.e., telephone_user.search("#")
  3. You can then conditionally format the phone number based on #2, e.g. if it's the 12th digit, format telephone_user with the country code. Also, with the switch-case, I would just do the 3-4 valid cases, and default on everything else.
Amy Yang
  • 1
  • 2