Numeral-js: Doesn't support phone numbers

Created on 28 May 2014  路  8Comments  路  Source: adamwdraper/Numeral-js

Tried doing this but it didn't format.
numeral(6095613928).format('(000)000-0000')

format

Most helpful comment

I added this "custom format" to my code to get it working. Borrowed from https://github.com/stevekinney/node-phone-formatter

```
import numeral from 'numeral';

numeral.register('format', 'phone', {
regexps: {
format: /+?N?[.\ -]?(?NNN)?[.\ -]?NNN[.\ -]?NNNN/
},
format : function (value, formatString) {
function normalize(phoneNumber) {
return phoneNumber.toString().replace(
/^[+\d{1,3}-\s]*(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
"$1$2$3"
);
}

function format(phoneNumber, formatString) {
  phoneNumber = normalize(phoneNumber);
  for (var i = 0, l = phoneNumber.length; i < l; i++) {
    formatString = formatString.replace("N", phoneNumber[i]);
  }
  return formatString;
}

return format(value, formatString);

}
});

And now I can do:

numeral(7861231234).format('(NNN) NNN-NNNN')
```

All 8 comments

+1

+1

+1

+1

+1

+1

+1

I added this "custom format" to my code to get it working. Borrowed from https://github.com/stevekinney/node-phone-formatter

```
import numeral from 'numeral';

numeral.register('format', 'phone', {
regexps: {
format: /+?N?[.\ -]?(?NNN)?[.\ -]?NNN[.\ -]?NNNN/
},
format : function (value, formatString) {
function normalize(phoneNumber) {
return phoneNumber.toString().replace(
/^[+\d{1,3}-\s]*(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
"$1$2$3"
);
}

function format(phoneNumber, formatString) {
  phoneNumber = normalize(phoneNumber);
  for (var i = 0, l = phoneNumber.length; i < l; i++) {
    formatString = formatString.replace("N", phoneNumber[i]);
  }
  return formatString;
}

return format(value, formatString);

}
});

And now I can do:

numeral(7861231234).format('(NNN) NNN-NNNN')
```

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amirtgm picture amirtgm  路  6Comments

jonathanbergson picture jonathanbergson  路  6Comments

zzzgit picture zzzgit  路  5Comments

anselms picture anselms  路  4Comments

meafmira picture meafmira  路  8Comments