RegEx for swiss phone number

I need RegEx to check (using Laravel, php framework) for a swiss phone number that should match this format:

+41 11 111 11 11

The “+41” part should be exactly that way, while the rest (11 111 11 11) can be any number from 1 to 9.

The function that calls RegEx is as follows:

    $regex = "thisIsWhereINeedYourHelp";

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

Thank you for your help!

+4
source share
4 answers

Here is a regex that works great with a Swiss phone number:

^(\+?)(\d{2,4})(\s?)(\-?)((\(0\))?)(\s?)(\d{2})(\s?)(\-?)(\d{3})(\s?)(\-?)(\d{2})(\s?)(\-?)(\d{2})
+3
source

You can do it like:

(\+41)\s(\d{2})\s(\d{3})\s(\d{2})\s(\d{2})

Demo: http://regex101.com/r/hJ9oY0

+2

( ):

// input string cleanup
$input = preg_replace('/[^0-9+\(\)-]/', '', $input);

I, :

// swiss telephone validation +41 (0)xx xxx xxxx
if(preg_match('/^(\+41|0041|0){1}(\(0\))?[0-9]{9}$/',$input))
        $result = "match: CH";
else    $result = "no match";

0
source

You can use the Laravel-Phone package to check, format, and other functions.

In your case, you can specify your country as follows:

'phone' => 'required|phone:CH'

CH = Switzerland (Confoederatio Helvetica)

0
source

Source: https://habr.com/ru/post/1536130/


All Articles