How to Validate Phone Number Using PHP

How to validate phone number using PHP?

Since phone numbers must conform to a pattern, you can use regular expressions to match the entered phone number against the pattern you define in regexp.

php has both ereg and preg_match() functions. I'd suggest using preg_match() as there's more documentation for this style of regex.

An example

$phone = '000-0000-0000';

if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) {
// $phone is valid
}

Mobile number validation pattern in PHP

Mobile Number Validation

You can preg_match() to validate 10-digit mobile numbers:

preg_match('/^[0-9]{10}+$/', $mobile)

To call it in a function:

function validate_mobile($mobile)
{
return preg_match('/^[0-9]{10}+$/', $mobile);
}

Email Validation

You can use filter_var() with FILTER_VALIDATE_EMAIL to validate emails:

$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}

To call it in a function:

function validate_email($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

However, filter_var will return filtered value on success and false on failure.

More information at http://www.w3schools.com/php/php_form_url_email.asp.

Alternatively, you can also use preg_match() for email, the pattern is below:

preg_match('/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/', $email)

Regex to Validate Phone Numbers in php

One option could be using optional parts at the start of the pattern:

^\+?0*(?:91-)?\K(?:91)?[6-9][0-9]{9}$
  • ^ Start of string
  • \+?0*(?:91-)? Optionally match +, 0+ times a 0 or 91-
  • \K Forget what was matched
  • (?:91)? Optionally match 91
  • [6-9][0-9]{9} Match a digit 6-9 and 9 digits 0-9
  • $ End of string

Regex demo | Php demo

If you don't want to use anchors ^ and $ you could use lookaround assertions to make sure what is directly on the left and right is not a non whitespace char:

(?<!\S)\+?0*(?:91-)?\K(?:91)?[6-9][0-9]{9}(?!\S)

Regex demo | Php demo

Validate phone number in php

Your RegEx is wrong, there's an useless backslash before the 1. Correct:

/^168[0-9]{9}$/

Full code:

$phoneNumber = '168921500789';
if( preg_match( "/^168[0-9]{9}$/", $phoneNumber ) ){
echo "Valid number";
} else {
echo "Invalid number";
}

How to validate phone number in laravel 5.2?

One possible solution would to use regex.

'phone' => 'required|regex:/(01)[0-9]{9}/'

This will check the input starts with 01 and is followed by 9 numbers. By using regex you don't need the numeric or size validation rules.

If you want to reuse this validation method else where, it would be a good idea to create your own validation rule for validating phone numbers.

Docs: Custom Validation

In your AppServiceProvider's boot method:

Validator::extend('phone_number', function($attribute, $value, $parameters)
{
return substr($value, 0, 2) == '01';
});

This will allow you to use the phone_number validation rule anywhere in your application, so your form validation could be:

'phone' => 'required|numeric|phone_number|size:11'

In your validator extension you could also check if the $value is numeric and 11 characters long.

regex to validate phone number

^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$

Explanation;

  • ^ Asserts start of string
  • \+? Matches an optional plus
  • (\d[.\- ]*){9,14} between 9 and 14 single digits, possibly seperated by spaces, dots, or dashes.
  • (e?xt?\d{1,5})? Optionally a x possibly preceeded by an e or followed by a t. The letters always followed by between 1 and 5 numbers.
  • $ Asserts end of the string

Phone Number Regex Conditions

I would use this regex pattern for Singapore numbers:

^(?:\+65)?[689][0-9]{7}$

Sample script:

$number = "+6587654321";
if (preg_match("/^(?:\+65)?[689][0-9]{7}$/", $number)) {
echo "MATCH";
}


Related Topics



Leave a reply



Submit