In AngularJS, I need to format phone numbers while typing them. I do not want to use any library, as it should be straightforward.
I need a format: 99 99 99 99 99
var phone = tel.replace(/\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*/, '$1 $2 $3 $4 $5');
But it only formats my number as soon as it has been completely dialed. How can I make this regex work when the number is not yet complete?
Then I tried this:
var phone = tel.replace(/\D*(\d{2})\D*(\d{0,2})?\D*(\d{0,2})?\D*(\d{0,2})?\D*(\d{0,2})?\D*/, '$1 $2 $3 $4 $5');
But this is obviously the addition of non-required spaces.
source
share