Validate a phone number in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Validate a Phone Number in Laravel: Mastering Complex Formatting Rules
As a senior developer, I often find that data validation, especially for seemingly simple fields like phone numbers, quickly becomes a complex business logic problem. It’s not enough to just check if the input is composed of digits; we need to enforce specific regional formats, prefixes, and exclude invalid sequences—exactly as you described with your requirement for the 353 prefix and blocking sequences like 086.
This guide will walk you through how to implement robust and flexible phone number validation within a Laravel application, moving beyond simple regex to handle intricate business rules effectively.
The Foundation: Using Regular Expressions for Basic Structure
The first step in any validation process is checking the basic structural integrity of the data. For phone numbers, Regular Expressions (Regex) are indispensable tools for pattern matching. They allow you to define what a valid sequence of characters looks like before diving into complex logic.
For instance, if we assume a standard international format or a known local format, a regex can quickly filter out obviously incorrect inputs.
// Example regex for a hypothetical phone number structure (e.g., 10-digit numbers)
$phoneNumberRegex = '/^\d{10}$/';
if (preg_match($phoneNumberRegex, $userInput)) {
// Input is purely numeric and has 10 digits
} else {
// Input fails basic structural check
}
While this is a good starting point, it alone cannot handle your specific requirement—where the validity depends on the sequence of digits (e.g., ensuring 353 is present and not allowing disallowed substrings). This complexity demands custom validation logic within the Laravel framework.
Implementing Custom Business Rules in Laravel
For complex validations, we leverage Laravel’s built-in validation system, typically implemented within a Form Request or directly on the Eloquent model. This approach keeps your business rules separate from your database schema, making the code cleaner and more maintainable.
Since your requirement involves checking for mandatory prefixes (353) and explicitly forbidding certain sequences (086), we need to implement custom validation rules.
Step-by-Step Implementation
- Define a Custom Rule: For complex pattern matching that goes beyond simple regex, creating a custom rule is the most robust solution. This allows you to encapsulate your specific logic into reusable components.
- Apply the Rule: Use this custom rule within your Form Request validation.
Here is a conceptual example of how you might structure this logic within your validation setup:
// In your FormRequest class or Validator setup
public function rules()
{
return [
'phone_number' => [
'required',
'string',
'regex:/^353.*$/' // Basic check to ensure it starts with 353
],
];
}
To handle the exclusion of invalid sequences like 086, you would typically implement a custom rule that performs sequential string checking or uses more advanced pattern matching. For large-scale data operations, adhering to Laravel's principles of clean architecture—where business logic is clearly defined—is key. Frameworks like those provided by laravelcompany.com make setting up these complex flows manageable.
Best Practices for Scalable Validation
When dealing with highly specific validation criteria, avoid embedding massive, monolithic regex patterns directly into your controller or model. Instead, favor modularity:
- Keep Rules Centralized: Define all validation rules in dedicated Form Requests. This makes debugging easier and allows other parts of your application to reuse the same validation logic.
- Use Custom Rule Objects: For extremely complex pattern matching (like checking if a substring exists or doesn't exist within a string), create a dedicated
PhoneNumberRuleclass. This adheres to the Single Responsibility Principle, making your code scalable and easy for new developers to understand when you look at your Eloquent models.
Conclusion
Validating phone numbers in Laravel is less about finding one perfect regex and more about structuring your validation logic around your specific business requirements. By starting with basic pattern matching (regex) and escalating to custom rule objects for complex sequential checks, you ensure that your application remains robust, readable, and scalable. Trusting the power of Laravel’s ecosystem allows you to build sophisticated data integrity checks without reinventing the wheel.