Pregmatch telephone number

Ik want to make a check for a telephone number.
But I’m doing something wrong.
I wannt these numbers to be good:
06-12345678
012-123456789 (first number has to be a 0)
0123-123456 (first number has to be a 0)

This is what I have:

if (!isset($_POST['telefoon']) or (!preg_match('~^[0-0]{1}[0-9]{1,3}[-]?[0-9]{6,8}$~D', $_POST['telefoon']) && strlen($_POST['telefoon']) != 11) or trim($_POST['telefoon']) == '')

This sounds remarkably like a homework question we’ve answered before.

But, making a regex is simply two steps:
Define your Rules.
Convert the rules to Regex.

So what do you want… here’s my interpretation of what you’ve written. See if i’m right.
A valid phone number is…
“Exactly two digits, followed by a hyphen, followed by exactly 8 digits.”
OR
“A 0, followed by exactly 2 digits, a hyphen, and then exactly 9 digits”
OR
“A 0, followed by exactly 3 digits, a hyphen, and then exactly 6 digits”

What you’ve got written in your code,

[0-0]{1}[0-9]{1,3}[-]?[0-9]{6,8}

Says "Exactly 1 “0” (this can be simplified into just “0{1}”), followed by 1 to 3 digits, an optional hyphen, and then 6 to 8 digits".
But that adds more combinations of numbers that are valid;
“0000000000” is a valid phone number according to your regex.

1 Like

Instead of regular expressions I would recommend you use libphonenumber to validate phone numbers, as this actually detect whether phone numbers are correct for different countries based on the rule for that particular country.

Thank you, that is what my plan was. The - should not be optional.

But maybe it is beter to just allow 10 numbers, it’s much easyer.

@rpkamp, nice programme, thank you for sharing, but maybe a little bit to much for just 1 form?
I will read more about it. For now I will try the normal normal preg_match.

I make this website for a non profit organisation.

Because you want optional patterns… you want optional patterns. (“Huh?”)

patternA | patternb | patternc.

pattern A is “Exactly two digits, followed by exactly 1 hyphen, followed by exactly 8 digits.”
pattern B is “Exactly one 0, followed by exactly 2 digits, exactly 1 hyphen, and then exactly 9 digits”
pattern C is “Exactly one 0, followed by exactly 3 digits, exactly 1 hyphen, and then exactly 6 digits”

All three patterns should look from the start (^) to the end ($) of the field.

Based on my description of what your pattern says in post #2 (I went back and colorized it to make it easier to match up), you should now be able to put together the requisite patterns from the building blocks. Give it a try and let us know what you come up with.

Ok with this info and your tips, I think it should be something like this:

(^)[0]{1}($)(^)[0-9]{1,3}($)[-]?(^)[0-9]{6,8}($)

Ow the partern should be like this:

pattern A is “Exactly one 0, followed by exactly 1 digit, exactly 1 hyphen, and then exactly 8 digits.”
pattern B is “Exactly one 0, followed by exactly 2 digits, exactly 1 hyphen, and then exactly 7 digits”
pattern C is “Exactly one 0, followed by exactly 3 digits, exactly 1 hyphen, and then exactly 6 digits”

You’ve got the right idea, but two things:

Dont try and combine the patterns, or you’ll end up in trouble. Write pattern A, B, and C seperately, and then stick them together as i showed you in post #5: A|B|C (the pipe is a literal pipe character, |) because right now i could say 0999-99999999 is a valid phone number. (| means “OR” for regex)
the ^ goes at the beginning of each pattern, and the $ at the end, but nowhere inbetween, and does not require parenthesis.

Also, make sure to remove any character that is not a space or a digit to make it a bit less strict. Otherwise it also won’t allow spaces for example, which a lot of people use in phone numbers.

I’m not sure how that works with the removing/replacing of spaces.

But this is how far I got sofar:

if (!isset($_POST['telefoon']) or (!preg_match('^[0]{1}[0-9]{1}[-][0-9]{8}$', $_POST['telefoon']) or !preg_match('^[0]{1}[0-9]{2}[-][0-9]{7}$', $_POST['telefoon']) or !preg_match('^[0]{1}[0-9]{3}[-][0-9]{6}$', $_POST['telefoon'])or trim($_POST['telefoon']) == '') && strlen($_POST['telefoon']) != 11) 
{
}
else
{
}

You’re close enough that you’ve learned the lesson (basic regex) that i was trying to teach :wink: so try this.

preg_match('/^0[0-9]{1}-[0-9]{8}$|^0[0-9]{2}-[0-9]{7}$|^0[0-9]{3}-[0-9]{6}$/', $_POST['telefoon'])

If you wanted to get fancy;

preg_match('/^0\d(-\d{2}|\d-\d|\d{2}-)\d{6}$/', $_POST['telefoon'])

(Because the three patterns share the common elements of 6 digits at the end, and a 0 and 1 digit at the start, the pattern can be sub-written as the differing component. \d is a shortcut for [0-9].)

1 Like

Thanks I understand it now.

I changed it a bit further.

The second number can never be a 0.
So

preg_match('/^0[1-9](-\d{2}|\d-\d|\d{2}-)\d{6}$/', $_POST['telefoon'])