Skip to content

Phone number validation for Laravel 4 and 5

License

Notifications You must be signed in to change notification settings

giggsey/Laravel-Phone

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Phone Validator

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

Adds a phone validator to Laravel 4 and 5 based on the PHP port of Google's libphonenumber API by giggsey.

Installation

  1. In the require key of composer.json file add the following

    "propaganistas/laravel-phone": "~2.0"
  2. Run the Composer update command

    $ composer update
  3. In your app config, add 'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider' to the end of the $providers array

    'providers' => [
        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        ...
        'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',
    ],
  4. In your languages directory, add for each language an extra language line for the validator:

"phone" => "The :attribute field contains an invalid number.", ```

Usage

To validate a field using the phone validator, use the phone keyword in your validation rules array. The phone validator is able to operate in two ways.

'phonefield' => 'phone:US,BE', ```

The validator will check if the number is valid in at least one of provided countries, so feel free to add as many country codes as you like.

  • Or you don't specify any parameters but you plug in a dedicated country input field (keyed by ISO 3166-1 compliant country codes) to allow end users to supply a country on their own. The easiest method by far is to install the CountryList package by monarobase. The country field has to be named similar to the phone field but with _country appended:

'phonefield' => 'phone', 'phonefield_country' => 'required_with:phonefield', ```

If using the CountryList package, you could then use the following snippet to populate a country selection list:

```php

Countries::getList(App::getLocale(), 'php', 'cldr')) ```

To specify constraints on the number type, just append the allowed types to the end of the parameters, e.g.:

'phonefield'  => 'phone:US,BE,mobile',

The most common types are mobile and fixed_line, but feel free to use any of the types defined here.

Display

Format a fetched phone value using the helper function:

phone_format($phone_number, $country_code, $format = null)

The $format parameter is optional and should be a constant of \libphonenumber\PhoneNumberFormat (defaults to \libphonenumber\PhoneNumberFormat::INTERNATIONAL)

About

Phone number validation for Laravel 4 and 5

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%