I need regular express for US Phone Number format.I want to replace the phone number string into below US phone number string format in JavaScript.
var number = "4031234789";
And I want to mask it in below format:-
number = number.mask('(000) 000-0000');
Can anyone show me a regular expression for that in JavaScript?
You can use regular expression and then concatenate to form your string.
Use phone number masking plugins for that.
Check the following plugins:
http://wiki.jqueryui.com/w/page/12137996/Mask
http://igorescobar.github.io/jQuery-Mask-Plugin/
http://digitalbush.com/projects/masked-input-plugin/
You can use my package https://github.com/jsonmaur/coverup, which can mask the input string onblur and maintain the symbols in the string.
This answer assumes you want the following format:
(000) 000-0000
(as the OP states).There are multiple different ways to implement this, but here are a couple different approaches:
If you want to simply mask the number on the
blur
event (when the field losesfocus
), then you could use the following:Alternatively, if you would rather mask the number while typing, you can listen to the
input
event and then conditionally mask the number based on the regex match: