Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nspaul's avatar
Level 11

Form Request validation rule, 10 or 16 digits - use pipe for either/or?

I have a use case that is somewhat unique but not particularly weird, and I thought I'd get some input from the community since I can't find a clear answer. I have a FormRequest validating an input field on a form. I'd like to enforce a field to be numeric and have either 10 or 16 digits. Not a range of digits, but specifically those two options.

I found a regex that works, shown here:

public function rules()
    {
        return [
            'MyDigitField' => ['nullable','regex:/^(\d{10}|\d{16})$/'],
        ];
    }

However, instead of regex:/^(\d{10}|\d{16})$/, I'd love to use the format digits:10|16 or digits:10|digits:16, but these do not seem to work from my testing. Does anyone know an elegant way to do this without the regex?

0 likes
2 replies
nspaul's avatar
Level 11

I wouldn’t say that I “don’t like” regex. I realize the power and will use it when necessary. But for some cases, like the one described in my original post, I think we’d all agree the digits:10|16 or digits:10|digits:16 is very readable by most people.

Please or to participate in this conversation.