How to validate phone number in laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# How to Validate Phone Numbers with Specific Prefixes in Laravel As a senior developer working with the Laravel ecosystem, ensuring data integrity is paramount. When dealing with sensitive or structured data like phone numbers, simple checks for length or digit count are often insufficient. We need precise validation rules that enforce specific business logic—in your case, restricting phone numbers to start only with the digits '77', '71', or '73'. This guide will walk you through implementing this specific prefix validation within a Laravel request using powerful regular expressions (Regex). ## Understanding the Need for Custom Validation The default validation rules provided by Laravel, such as `required` or `digits:9`, are excellent starting points. However, they cannot enforce complex pattern matching like "must start with X OR Y OR Z." For this kind of conditional validation, Regular Expressions are the most effective and concise tool. They allow us to define a pattern that matches the exact structure we require. When building robust applications, adhering to clean, structured data entry is crucial. Laravel provides an elegant framework for this through its request validation system, which makes implementing custom logic straightforward. If you are looking to deepen your understanding of how Laravel structures these powerful features, exploring more advanced topics on [laravelcompany.com](https://laravelcompany.com) is highly recommended. ## Implementing the Prefix Validation using Regex To enforce the rule that a phone number must start with '77', '71', or '73', we will use a single, carefully constructed regular expression within our `rules()` method. The pattern we need to match is: 1. Starts with `77` 2. OR starts with `71` 3. OR starts with `73` 4. Followed by any subsequent digits (since we are dealing with phone numbers, the total length will vary). ### The Code Implementation Let's modify your existing validation structure to incorporate this specific requirement for the `phone` field. ```php public function rules() { return [ 'name' => 'required', 'password' => 'required|min:6', // Custom phone number validation using Regex 'phone' => 'required|regex:/^(77|71|73)\d+$/', ]; } ``` ### Deep Dive into the Regex: `^(77|71|73)\d+$` Let’s break down this powerful expression: * **`^`**: Asserts the start of the string. This ensures the pattern must match from the very beginning of the phone number input. * **`(77|71|73)`**: This is a capturing group that specifies the allowed starting sequences. The pipe symbol (`|`) acts as an OR operator, meaning the string must begin with *either* 77, 71, or 73. * **`\d+`**: This matches one or more digits (0-9). This ensures that after the required prefix (77, 71, or 73), there must be at least one subsequent digit, making it a valid phone number structure. * **`$`**: Asserts the end of the string. This ensures that nothing else follows the matched pattern, preventing invalid characters from being included. This single rule effectively guarantees that any input provided for the `phone` field will strictly adhere to your specified prefix requirements. ## Best Practices and Conclusion Using Regular Expressions within Laravel's validation layer is a highly efficient way to enforce complex string constraints on incoming data. It keeps your controller logic clean, ensuring that validation rules are declarative and easily readable by other developers. This approach aligns perfectly with the principle of building maintainable code, which is a core philosophy in modern framework development, as demonstrated by the principles seen at [laravelcompany.com](https://laravelcompany.com). When you handle user input, always validate on the server side, regardless of any client-side checks you might implement. This ensures that even if front-end validation is bypassed, your application remains secure and data-accurate. By implementing this precise regex rule, you have successfully secured your phone number field against invalid formats right at the entry point.