Laravel 5.4 - Validation with Regex
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
In this blog post, we will explore how to create validation rules using regular expressions in Laravel 5.4.
Before moving forward, it is essential to understand that Laravel 5.4 has been end of life since August 2018 and no longer receives updates or bug fixes. For up-to-date solutions, we recommend using a newer version of the framework.
Validation with Regex in Laravel 5.4
Laravel provides robust validation functionality for input data. One way to define your validation rules is by using regular expressions. In our case, we want to ensure the project name starts with a letter from 'a-z' or 'A-Z' and can be followed by numbers but not both together. Let's take a look at an example:php
$this->validate(request(), [
'projectName' => 'required|regex:/(^([a-zA-z]+)(\d+)?$)/u',
];(?P<StartLetter[a-zA-Z]*).(?P<Numbers>\d*)?(?=(\d+)?$)(?>\1[\3\d]*(?=\1))
This rule is broken into four parts: 1. (?PNote: This approach might look slightly complex but using these advanced regular expressions can efficiently validate user inputs based on specific criteria.
To verify this rule, we can use a regex testing tool like https://regex101.com/r/FylFY1/2. Here's the link we mentioned in our original question: https://regex101.com/r/FylFY1/2