How to validate phone number with zero at the front in php?

Member

by denis , in category: PHP General , 9 months ago

How to validate phone number with zero at the front in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 9 months ago

@denis 

You can validate a phone number with a zero at the front in PHP using regular expressions. Here's an example code snippet to validate a phone number with a zero at the front:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php

$phone_number = '0123456789';

// Regular expression to validate phone number with zero at the front
$pattern = '/^0[1-9]d{8}$/';

if (preg_match($pattern, $phone_number)) {
    echo "Phone number is valid!";    
} else {
    echo "Phone number is invalid!";
}

?>


In this code snippet, the regular expression pattern '/^0[1-9]d{8}$/' will match a phone number that starts with zero, followed by a digit between 1 and 9, and then followed by 8 digits.


You can customize the regular expression pattern according to your specific requirements for validating phone numbers with zeros at the front.