Laravel 5.4 - Validation with Regex [duplicate]

Solution 1:

Your rule is well done BUT you need to know, specify validation rules with regex separated by pipeline can lead to undesired behavior.

The proper way to define a validation rule should be:

$this->validate(request(), [
    'projectName' => 
        array(
            'required',
            'regex:/(^([a-zA-Z]+)(\d+)?$)/u'
        )
];

You can read on the official docs:

regex:pattern

The field under validation must match the given regular expression.

Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.