Regex for phone number with specific area code and format

4k views Asked by At

I'm trying to create a regular expression that would validate a specific phone number with an 868 area code and particular format h thus I want these

8680000000

(868) 000 0000

868-000-0000

868 000-0000

to pass validation but these

8880000000

767-000-0000

868-0000000

(868)0000000

to fail validation.

So far I got:

^((\(){0,1}(868){1}(\)){0,1})+:?((((\ |\-)(\d{3})(\ |\-)):?\d{4})+|(\d{4,7}))

But

(868)0000000 

is validating

6

There are 6 answers

1
Rakholiya Jenish On BEST ANSWER

Actually this is somewhat hard-coded because you are having some specific requirements like validating 868-000-0000 but not 868-0000000.

Regex is:

(^\(868\)\ \d{3}\ \d{4})|(^868\d{7})|(^868\-\d{3}\-\d{4})|(^868\ \d{3}\-\d{4})

DEMO

0
Ed Morton On

Assuming PHP uses EREs like awk or grep -E does:

$ grep -E '^((868([0-9]{3}|[- ][0-9]{3}-))|(\(868\) [0-9]{3} ))[0-9]{4}$' file
8680000000
(868) 000 0000
868-000-0000
868 000-0000

or if you like \d instead of [0-9]:

^((868(\d{3}|[- ]\d{3}-))|(\(868\) \d{3} ))\d{4}$

Anything briefer would match other phone number formats that you didn't specify as being valid and so I'm assuming are invalid.

8
Sammitch On

I genuinely do not understand why everyone need to validate a phone number with strict spacing, bracketing, hyphenation, and god-knows-what-else requirements.

Strip out all unnecessary characters, validate what needs validating, move on with life.

$phone = '(868) 555-1234'; // or 868 555.1234 or +868x55-5-123-4 etc
$phone = preg_replace('/[^0-9]/', '', $phone); // 8685551234
if( substring($phone, 0, 3) == '868' ) {
  // great success
}

Bonus round. Format your data for hu-man display:

$phone_display = sprintf('(%s) %s-%s',
  substring($phone,0,3),
  substring($phone,3,3),
  substring($phone,6,4));
1
splash58 On

Such regex

^(\(?868\)?(\s*|-)\d{3}(\s|-)\d{4}|868\d{7})$

Validate these

8680000000
(868) 000 0000
868-000-0000
868 000-0000

Not validate

8880000000
767-000-0000
868-0000000
(868)0000000

match[1] contains number

DEMO and explanation of the regex

0
AudioBubble On

You can do this with some conditionals

 # ^(\()?868(?(1)\)[ ]\d{3}[ ]\d{4}|(?:(?:(-)|([ ])|)\d{3}(?(2)\2|(?(3)[ -]))\d{4}))$

 ^                             # BOS
 ( \( )?                       # (1), Form (xxx) xxx xxxx
 868                           # 868 Area Code
 (?(1)                         # Need Form 1 ?
      \)
      [ ] \d{3} [ ] \d{4} 
   |                              # else
      (?:
           (?:
                ( - )                         # (2), Form xxx-xxx-xxxx
             |  ( [ ] )                       # (3), Form xxx xxx[ -]xxxx
             |                              # Form xxxxxxxxxx
           )
           \d{3}      
           (?(2)                         # Need form 2 ?
                \2 
             |                              # else
                (?(3) [ -] )                  # Need form 3 ?
           )
           \d{4}      
      )
 )
 $                             # EOS

Passed:

8680000000
(868) 000 0000
868-000-0000
868 000-0000

Failed:

8880000000
767-000-0000
868-0000000
(868)0000000

0
Sverri M. Olsen On

You could write one regular expression that matches all the formats, but there is no rule that says you have to.

Matching each format separately makes your code quite a bit more readable:

function matchPhoneNumber($phoneNumber) {
    $regex = [
        '/^(?P<prefix>868)(?P<first>\d{3})(?P<second>\d{4})$/',
        '/^(?P<prefix>868)-(?P<first>\d{3})-(?P<second>\d{4})$/',
        '/^(?P<prefix>868)\s(?P<first>\d{3})-(?P<second>\d{4})$/',
        '/^\((?P<prefix>868)\)\s(?P<first>\d{3})\s(?P<second>\d{4})$/',
    ];
    foreach ($regex as $reg) {
        if (preg_match($reg, $phoneNumber, $match) === 1) {
            return $match;
        }
    }
    return false;
}