How do you filter (and replace) phone# input if javascript disabled and you want (XXX)XXX-XXXX exactly, in PHP or Regex - Stack

I have a jQuery that handles this very well using a mask, the problem is when users do not have javascr

I have a jQuery that handles this very well using a mask, the problem is when users do not have javascript. This is rare, but we want to have a fall back. Right now our regular expression fall back is a built in tool in the module, confirming 10 numbers and that is it. I would like an input of XXX-XXX-XXXX, an input of XXXXXXXXXX or anything other to be converted to (XXX)XXX-XXXX. I am willing to write code in PHP to process this, but if there is a regular expression or something already a standard I have not seen yet I would be interested in. The idea is finding something that is full proof so this can be passed into a database. An example of this form is at .

I have a jQuery that handles this very well using a mask, the problem is when users do not have javascript. This is rare, but we want to have a fall back. Right now our regular expression fall back is a built in tool in the module, confirming 10 numbers and that is it. I would like an input of XXX-XXX-XXXX, an input of XXXXXXXXXX or anything other to be converted to (XXX)XXX-XXXX. I am willing to write code in PHP to process this, but if there is a regular expression or something already a standard I have not seen yet I would be interested in. The idea is finding something that is full proof so this can be passed into a database. An example of this form is at http://www.gestationaldiabetic..

Share Improve this question asked Feb 27, 2012 at 17:36 ShaneShane 1,6503 gold badges23 silver badges51 bronze badges 1
  • you may take a look at this – dee Commented Feb 27, 2012 at 17:39
Add a ment  | 

3 Answers 3

Reset to default 6

I'm not a PHP guy, but the easiest thing to do is simply strip out everything but digits, and then recreate your string before passing into the DB.

Let the user type it in however they want as long as you get 10 digits.

Welp, I use the following regex:

^[^2-9]*([2-9])\D*(\d)\D*(\d)[^2-9]*([2-9])(?:\D*(\d)[^02-9]*([02-9])|[^02-9]*([02-9])\D*(\d))\D*(\d)\D*(\d)\D*(\d)\D*(\d).*$

and then use the replacement pattern:

$1$2$3-$4$5$6$7$8-$9$10$11$12

it allows any of these to be reformatted as indicated:

Input                                                                Output
---------------------                                                --------
2345678905                                                           234-567-8905
2345678905                                                           234-567-8905
234-569-8956                                                         234-569-8956
223.5876954                                                          223-587-6954
4444444444                                                           444-444-4444
 2359996574                                                          235-999-6574
 777-873 6542                                                        777-873-6542
      (800) 874 8321                                                 800-874-8321
223...587...9999                                                     223-587-9999
1-800-345-6734                                                       800-345-6734
8.5.3-2.9.7-6.5.4.3                                                  853-297-6543
+1 (976) 566-7763                                                    976-566-7763
   0154388640         .           -              33                  543-886-4033
%4*555@123$5^78                                                      455-512-3578
#445#2984$32@9                                                       445-298-4329
$123,456,987.20                                                      234-569-8720
 123,456,987.20                                                      234-569-8720
 $23,456,987.20$                                                     234-569-8720
3/9/1954 @21:33                                                      391-954-2133
12345678901234567890                                                 234-567-8901
55555555555555555555555555                                           555-555-5555
lkajshdf28jksa23:'\\\fg[]ewr]?.,,ewf3412233445566gwqerq              282-334-1223
$123,456,987.20 dollars                                              234-569-8720
$223,456,987.20                                                      223-456-9872
222-211-45674                                                        222-214-5674

Important Notes: it parses the first ten conforming digits that MIGHT represent a telephone number, effectively not allowing entry of a telephone number that does not conform to the rules stated in the NANP article on Wikipedia.

The five groups in the middle of the expression (groups 4-8) are the first digit (#4) of the "Exchange Code" and then the last two digits (#5/#7, and #6/#8, respectively) of the exchange code, if they are not both 1

Basically, any true, valid telephone number should be parsed and reformatted correctly.

Some other options are:

This allows getting and reformatting an extension (indicated by `x`, `ext.`, `extension`, plus a few variations of those and only if all extension digits are contiguous), if supplied:
Parsing Expression
------------------
(?i)^\D*1?\D*([2-9])\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)[^x]*?\s*(?:(?:e?(x)(?:\.|t\.?|tension)?)\D*(\d+))?.*$
Replacement
-----------
$1$2$3-$4$5$6-$7$8$9$10 $11$12


This does not enforce ALL of the NANP rules, but does most of them and is bug free, according to my tests:
Parsing Expression
------------------
^[^2-9]*([2-9])\D*(\d)\D*(\d)\D*([2-9])\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d).*$
Replacement
-----------
$1$2$3-$4$5$6-$7$8$9$10

I can explain further or give you another version if you'd like!

ALSO, there is something to be said for efficiency of the 'piled' code (JavaScript typically is not really piled, .Net and Java don't get FULLY piled and many languages are only parsed real time...), but there is also a lot to be said for pact source code - you decide whether you want to have an expression (like regex) do all the work for you, or create all the looping, checking, parsing etc. on your own.

Here's a dummy one.

function format_phone($num) {
    $num = preg_replace('/[^\d]/','',$num);
    if (strlen($num) != 10) return 0;
    // this requires the number to contain 10 digits
    return preg_replace('/^(\d{3})(\d{3})(\d{4})$/','($1)$2-$3',$num);
}

$res = format_phone('0123456789');
if (!$res) {
    echo 'Phone number should contain a 3-digit area code and a 7-digit local phone number.';
} else {
    echo $res;
    // INSERT the number in your DB
}

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信