Phone number validation in Laravel: Comprehensive Guide
- Validate Phone Numbers in Laravel
- Basic Phone Number Validation Using Built In Rules
- International Phone Number Validation
- Using Custom Validation Rules
- Validating Phone Numbers in Form Requests
- Using Third Party Libraries for Advanced Validation
- Summary
Validate Phone Numbers in Laravel
Phone number validation is a common requirement in modern applications. Clean and accurate phone data ensures reliable communication, proper user identity verification, and consistent database formatting. Laravel provides flexible tools for validating both simple and complex phone formats through built in rules, regex, custom rules, and third party libraries.
Basic Phone Number Validation Using Built In Rules
Using the required and numeric rules
For basic numeric phone input:
$request->validate([ 'phone' => ['required', 'numeric'],]);
This ensures the value contains only digits. It is useful for local formats without symbols or country codes.
Using regex for simple patterns
Regex allows basic structure enforcement. For example, a ten digit local number:
$request->validate([ 'phone' => ['required', 'regex:/^[0-9]{10}$/'],]);
Best practices when using regex
- Keep patterns minimal and readable.
- Avoid overly strict patterns that reject valid real world formats.
- Escape characters correctly to avoid unintended matches.
International Phone Number Validation
Understanding E.164 formatting
E.164 is the global standard for phone numbers. It requires a leading plus sign followed by up to fifteen digits. Example: +15551234567.
Regex for E.164 in Laravel validation
A reliable E.164 pattern:
$request->validate([ 'phone' => ['required', 'regex:/^\+[1-9][0-9]{1,14}$/'],]);
Explanation:
- The value must start with a plus sign.
- The first digit cannot be zero.
- The number can contain up to fifteen digits.
Common pitfalls
- Allowing spaces or punctuation when using E.164.
- Accepting country codes starting with zero.
- Forgetting to anchor the regex pattern at the start and end of the string.
Using Custom Validation Rules
Creating a custom rule
Generate a rule class:
php artisan make:rule PhoneNumber
Implementing passes and message
Example rule enforcing E.164:
namespace App\Rules; use Illuminate\Contracts\Validation\Rule; class PhoneNumber implements Rule{ public function passes($attribute, $value) { return preg_match('/^\+[1-9][0-9]{1,14}$/', $value) === 1; } public function message() { return 'The :attribute field must be a valid phone number.'; }}
Using the custom rule inside Form Requests
public function rules(){ return [ 'phone' => [new \App\Rules\PhoneNumber], ];}
This encapsulates validation logic and keeps controllers clean.
Validating Phone Numbers in Form Requests
Centralizing validation logic
Form Requests provide a dedicated location for validation. They improve maintainability and support custom messages, authorization, and reusable rules.
Example FormRequest with phone rules
namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class StoreUserRequest extends FormRequest{ public function rules() { return [ 'phone' => ['required', 'regex:/^\+[1-9][0-9]{1,14}$/'], ]; }}
Custom messages and attribute naming
public function messages(){ return [ 'phone.regex' => 'Enter a valid E.164 phone number.', ];} public function attributes(){ return [ 'phone' => 'phone number', ];}
Using Third Party Libraries for Advanced Validation
When to use a library
Use a library when you need country specific formatting, parsing, normalization, or validation beyond simple regex. This is helpful when supporting multiple regions.
Example using the propaganistas/laravel-phone package
Install the package:
composer require propaganistas/laravel-phone
Validate a phone number for a specific country:
$request->validate([ 'phone' => ['required', 'phone:US'],]);
Validate an international phone number:
$request->validate([ 'phone' => ['required', 'phone:INTERNATIONAL'],]);
Validate multiple possible countries:
$request->validate([ 'phone' => ['required', 'phone:US,CA'],]);
Summary
Laravel offers powerful options for validating phone numbers ranging from simple digit checks to advanced international validation. Developers can use built in rules for straightforward cases, regex for structured formats, custom rules for complex logic, and external libraries for global accuracy. These tools allow applications to collect clean, consistent, and reliable phone data across diverse regions and use cases.
Stay Updated.
I'll you email you as soon as new, fresh content is published.