Skip to content
Advertisement

How to check that fifth and sixth characters are a specific two-digit number?

I would like to validate phone numbers. The only valid phone numbers start like this: +36 20 | +36 30 | +36 31 | +36 70

My code looks like this, but now you can enter +36 21 or +36 71 which I would like to avoid. How can I check for a two digit number? Like 70 or 30.

$phone = '+36 70 123 4567';

if ( preg_match( '|^+36 [237][01] [1-9][0-9]{2} [0-9]{2}[0-9]{2}$|', $phone ) ) { 
    echo 'phone number is good';
}

Advertisement

Answer

If you want some very specific numbers:

/^+36(20|30|31).../

Using set notation ([237][01]) will open up way too many other possibilities as you’ve observed.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement