How to validate phone number in laravel 5.2?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Comprehensive Guide to Validating Phone Numbers in Laravel 5.2
Introduction:
Validating user input is an essential aspect of web application development, ensuring that you have accurate data for further processing and avoiding potential issues later on. In this blog post, we will discuss how to validate phone numbers in Laravel 5.2 applications using the built-in validation features. We'll also learn about specific conditions to ensure the input is strictly as per our requirements - specifically, starting with '01', having exactly 11 digits, and being a number only.Prerequisites:
To follow this guide, you need to have basic knowledge of Laravel framework (version 5.2) and PHP. You should also be familiar with creating controllers and validating user inputs. If you're new to Laravel, check out our comprehensive tutorials on Getting Started with this framework.The Problem:
Suppose you want to build an application where users need to register and provide their phone number for future communication. You want the number to satisfy these conditions: 1. Starts with '01' (for example, 01234567890) 2. Be exactly 11 digits long 3. Contain only numeric characters (no alphabets or special symbols)The Solution:
To address these requirements in Laravel 5.2, we can utilize the built-in validation rules and custom conditions as shown in the following example code:php
public function saveUser(Request $request){
$this->validate($request,[
'name' => 'required|max:120',
'email' => 'required|email|unique:users',
'phone' => 'required|min:11|numeric',
'course_id'=> 'required'
]);
$user = new User();
$user->name= $request->Input(['name']);
$user->email= $request->Input(['email']);
$user->phone= $request->Input(['phone']);
$user->date = date('Y-m-d');
$user->completed_status = '0';
$user->course_id=$request->Input(['course_id']);
$user->save();
return redirect('success');
}Conclusion:
Following this guide, you can easily validate phone numbers in Laravel 5.2 applications while meeting specific conditions. By implementing proper validation rules and custom conditions, you're guaranteed to have accurate data for your application needs. Remember, a correct and thorough understanding of the framework is critical in building robust web applications with secure user data handling.