I need to check / replace a Phone number in a Form Field with regex and it should really be simple. I can't find Solutions in this Format:
"place number"
so: "0521 123456789"
Nothing else should work. No special Characters, no country etc.
Just "0521 123456789"
Would be great if someone could provide a solution as I'm not an Expert with regex (and PHP).
You can use the following RegEx:
^0[1-9]\d{2}\s\d{9}$
That will match it how it is exactly
How it works:
^ # String Starts with ...
0 # First Digit is 0
[1-9] # Second Digit is from 1 to 9 (i.e. NOT 0)
\d{2} # 2 More Digits
\s # Whitespace (use a [Space] character instead to only allow spaces, and not [Tab]s)
\d{9} # Digit 9 times exactly (123456789)
$ # ... String Ends with
PHP
code:
$regex = '~\d{4}\h\d{9}~';
$str = '0521 123456789';
preg_match($regex, $str, $match);
See a demo on ideone.com.
To allow only this pattern (i.e. no others characters), you can anchor it to the beginning and end:
$regex = '~^\d{4}\h\d{9}$~';