Formatting phone number with Regex in Javascript - Stack Overflow

I am creating a PDF form in Adobe Acrobat Pro and would like to format phone numbers using javascript s

I am creating a PDF form in Adobe Acrobat Pro and would like to format phone numbers using javascript so that a space is included after the area code and a hyphen is added after the prefix. I want to allow the user to enter extensions and other relevant info after the 10-digit phone number.

Below are a few examples of how phone numbers should be formatted:

Number Entered        Formatted Version
2125551212            212 555-1212
(212) 555 1212        212 555-1212
212-555-1212 ext. 101 212 555-1212 ext. 101
212 555 1212 x101     212 555-1212 x101

The code below should first strip out any spaces, hyphens, and other characters that are not digits or letters. That part seems to work OK, but the reformatting part of the code does not work properly. When a user adds an extension, the formatting is pletely off. For example, 212 5551212 ext 339 bees 212 5-5--1212 ext339.

{
f_val = event.value.replace(/\D\W[^\.]/g, "");
event.value = f_val.slice(0,3)+" "+f_val.slice(3,6)+"-"+f_val.slice(6);
}

Any thoughts or suggestions would be greatly appreciated.

(Please note, I am aware that this code does not validate and do not currently want it to do so.)

I am creating a PDF form in Adobe Acrobat Pro and would like to format phone numbers using javascript so that a space is included after the area code and a hyphen is added after the prefix. I want to allow the user to enter extensions and other relevant info after the 10-digit phone number.

Below are a few examples of how phone numbers should be formatted:

Number Entered        Formatted Version
2125551212            212 555-1212
(212) 555 1212        212 555-1212
212-555-1212 ext. 101 212 555-1212 ext. 101
212 555 1212 x101     212 555-1212 x101

The code below should first strip out any spaces, hyphens, and other characters that are not digits or letters. That part seems to work OK, but the reformatting part of the code does not work properly. When a user adds an extension, the formatting is pletely off. For example, 212 5551212 ext 339 bees 212 5-5--1212 ext339.

{
f_val = event.value.replace(/\D\W[^\.]/g, "");
event.value = f_val.slice(0,3)+" "+f_val.slice(3,6)+"-"+f_val.slice(6);
}

Any thoughts or suggestions would be greatly appreciated.

(Please note, I am aware that this code does not validate and do not currently want it to do so.)

Share Improve this question asked Aug 11, 2014 at 15:59 KenKen 3,14112 gold badges46 silver badges72 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

If all the phone numbers are always like in your example, you can use a single replacement:

var re = /\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})/g; 
var subst = '$1 $2-$3'; 

var result = str.replace(re, subst); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745332709a4622957.html

相关推荐

  • Formatting phone number with Regex in Javascript - Stack Overflow

    I am creating a PDF form in Adobe Acrobat Pro and would like to format phone numbers using javascript s

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信