I want to validate a given international phone number using php preg_match() function following these rules: - Only digits allowed, no special chars and white spaces
phone number must starts always with '00' not +
phone number length should be not lower than 11 symbols
So far I have this which checks the beginning of the string:
preg_match("~^00\d+$~", $_POST['phone'])
but if I pass: 00014568978945 it will accept it as correct as it checks to has min '00' but third '0' makes the phone invalid..
How should look like the full preg_match() condition to check for above requirements? Thank you in advance for your time!
I suggest
~^00[1-9]\d{8,}$~D
See the regex demo.
Details:
^00
- starts with two 0
s, then[1-9]
- a non-zero digit, and then \d{8,}
- any 8 or more digits$
- end of string.Since you are validating, I also included D
PCRE_DOLLAR_ENDONLY modifier that forces the $
anchor to always match the very end of the string.