Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for swiss phone number

I need a RegEx to do the validation (using Laravel, a php framework) for a swiss phone number which has to fit this format:

+41 11 111 11 11

The "+41" part has to be exactly this way while the rest (11 111 11 11) can be any number between 1 and 9.

The function that invokes the RegEx looks like this:

    $regex = "thisIsWhereINeedYourHelp";

    if (preg_match($regex, $value)) {
               return true;
            } 
    return true;

Thanks for your help!

like image 515
user3524209 Avatar asked Apr 11 '14 14:04

user3524209


1 Answers

This is the pattern I use:

/(\b(0041|0)|\B\+41)(\s?\(0\))?(\s)?[1-9]{2}(\s)?[0-9]{3}(\s)?[0-9]{2}(\s)?[0-9]{2}\b/

it matches all the following:

+41 11 111 11 11
+41 (0) 11 111 11 11
+41111111111
+41(0)111111111
00411111111
0041 11 111 11 11
0041 (0) 11 111 11 11
011 111 11 11
0111111111

any of the spaces can be left out and it checks for the word and non word boundaries.

like image 79
ParrapbanzZ Avatar answered Sep 25 '22 10:09

ParrapbanzZ