shankar/country-phone-validator

Laravel package to validate phone numbers using country name or dial code

v1.0.1 2025-06-28 04:48 UTC

This package is auto-updated.

Last update: 2025-06-28 04:48:58 UTC


README

A Laravel package for validating mobile phone numbers across different countries with additional features.

Features

  • Validate phone numbers for 200+ countries
  • Check if a number belongs to a specific country
  • Get country code from phone number
  • Format phone numbers according to international standards
  • Support for custom validation rules
  • Easy integration with Laravel validation system

Installation

You can install this package via Composer:

composer require shankar/country-phone-validator

The package will be auto-discovered by Laravel. If you are not using package auto-discovery, add the service provider to your config/app.php:

'providers' => [
    // ...
    Shankar\CountryPhoneValidator\CountryPhoneValidatorServiceProvider::class,
],

Usage

Basic Phone Number Validation

use Shankar\CountryPhoneValidator\Rules\PhoneNumber;

$request->validate([
    'phone' => ['required', new PhoneNumber],
]);

Validate by Country Name

use Shankar\CountryPhoneValidator\Rules\ValidPhoneByCountryName;

$request->validate([
    'phone' => ['required', new ValidPhoneByCountryName('India')],
]);

Validate by Dial Code

use Shankar\CountryPhoneValidator\Rules\ValidPhoneByDialCode;

$request->validate([
    'phone' => ['required', new ValidPhoneByDialCode('+91')],
]);

Country Names and Dial Codes

You can retrieve the list of supported countries with their dial codes and validation regex using the Countries class:

use Shankar\CountryPhoneValidator\Countries;

$countries = Countries::all();
// Example output:
// [
//     [
//         'name' => 'India',
//         'dial_code' => '+91',
//         'regex' => '/^...$/'
//     ],
//     ...
// ]