ellisio/laravel-phone

A phone validator for Laravel using the free Twilio phone lookup service.

v8.0.0 2020-03-10 18:50 UTC

This package is auto-updated.

Last update: 2021-06-10 21:52:57 UTC


README

Latest Stable Version License Total Downloads Build Status 68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f656c6c6973696f2f6c61726176656c2d70686f6e652f6261646765732f6770612e737667 68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f656c6c6973696f2f6c61726176656c2d70686f6e652f6261646765732f636f7665726167652e737667

Introduction

A phone validator for Laravel 6+ using the free Twilio Lookup API.

This package gives developers the ability to validate phone numbers and format phone numbers. All data will be pulled from the Twilio Lookup API.

Validation can be configured to check if the number is valid, or if it is valid within a given list of ISO-3166-1 Alpha 2 country codes.

Installation

Install via composer:

composer require ellisio/laravel-phone

Add your Twilio credentials to your .env file. If you don't have a Twilio account you can register one here for free.

TWILIO_ACCOUNT_SID=xxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxx

Configuration

If you want to customize the configuration for this package, you can publish the config file to /config/phone.php by running the following command:

php artisan vendor:publish --provider=EllisIO/Phone/PhoneServiceProvider --tag=laravel-phone-config

If you want to customize the output of the validation messages, you can publish the translations file to /lang/vendor/laravel-phone by running the following command:

php artisan vendor:publish --provider=EllisIO/Phone/PhoneServiceProvider --tag=laravel-phone-translations

Usage

Phone validation

If you need to verify that the given number is valid and that is all, use the phone rule.

return [
    'phone' => 'required|phone',
];

Country validation

If you need to validate that the given number is valid in a list of countries, use the phone_country:US,CA rule. You can list as many ISO-3166-1 Alpha 2 as you wish delimited by a comma.

return [
    'phone' => 'required|phone_country:US,CA',
];

Formatting phone numbers

If you want to format the phone number using INTERNATIONAL_FORMAT use the following code:

Phone::formatNumber('5551234567');

Creating phone number objects

This library includes the ability to generate a Phone object. This object contains the following details about a number:

  • countryCode: ISO-3166 alpha 2 country code.
  • number: E.164 number.
  • formattedNumber: National formatted number.
$phone = Phone::getPhone('5551234567');
$phone->getNumber(); // Returns "+15551234567"
$phone->getNationalNumber(); // Returns "5551234567"
$phone->getFormattedNumber(); // Returns "(555) 123-4567"
$phone->getCountry(); // Returns "US"
$phone->getCountryCallingCode(); // Returns "1"

Handling invalid numbers

Sometimes you may have bad data, it happens. To handle this, simply check to see if null was returned.

if (! $phone = Phone::getPhone('123')) {
    echo "Invalid number provided.";
}

Support

Need help? Create an issue.