Text-mask: Unable to create a US phone number with extension

Created on 9 Nov 2016  路  3Comments  路  Source: text-mask/text-mask

Is it possible to make a part of the mask array optional, that would be so helpful.
This mask ['(', /[1-9]/, /\d/, /\d/, ')', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/, /( x\d{1,6})/ ] is not working for US phone numbers with extension such as (123) 123-4567 x123

question

Most helpful comment

There's an example here:


phoneMask(userInput) {
    let numbers = userInput.match(/\d/g);
    let numberLength = 0;
    if (numbers) {
      numberLength = numbers.join("").length;
    }

    if (numberLength > 10) {
      return ['(', /[1-9]/, /[1-9]/, ')', ' ', /\d/, /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/];
    } else {
      return ['(', /[1-9]/, /[1-9]/, ')', ' ', /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/];
    }
  }

Just change the if and the masks and you are good to go =) Then in your input:

[textMask]="{mask: phoneMask}"

All 3 comments

@ktriek Thanks for this request!

You would need to use a mask function for that.

You would create a mask function that returns a phone number mask until the user has entered a complete phone number, then if the user enter's an x, the mask function would expand the mask to accept more characters.

I'm afraid there isn't another easier way :)

There's an example here:


phoneMask(userInput) {
    let numbers = userInput.match(/\d/g);
    let numberLength = 0;
    if (numbers) {
      numberLength = numbers.join("").length;
    }

    if (numberLength > 10) {
      return ['(', /[1-9]/, /[1-9]/, ')', ' ', /\d/, /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/];
    } else {
      return ['(', /[1-9]/, /[1-9]/, ')', ' ', /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/];
    }
  }

Just change the if and the masks and you are good to go =) Then in your input:

[textMask]="{mask: phoneMask}"

Thanks @fernandobandeira!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kenpeter picture kenpeter  路  3Comments

skube picture skube  路  3Comments

jvbianchi picture jvbianchi  路  5Comments

msafi picture msafi  路  7Comments

lincolnthree picture lincolnthree  路  4Comments