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

tonz18's avatar

Laravel Validation with or

I have a laravel validation that needs to match one of the 3 regex's

$validatedData = $request->validate([

    'BCH-address' => 'regex:/^([13][a-km-zA-HJ-NP-Z1-9]{25,34})/',
    'BCH-address' => 'regex:/^((bitcoincash:)?(q|p)[a-z0-9]{41})/',
    'BCH-address' => 'regex:/^((BITCOINCASH:)?(Q|P)[A-Z0-9]{41})$/',

]),

Is there a way to use a OR condition for the validation??

0 likes
2 replies
tonz18's avatar

Ended up using a Closures

$validatedData = $request->validate([

            'BTC-address' => 'regex:/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/',
            'ETH-address' => 'regex:/^0x[a-fA-F0-9]{40}$/',
            'BCH-address' => function($attribute, $value, $fail) {
            if ( !preg_match("/^([13][a-km-zA-HJ-NP-Z1-9]{25,34})/",$value) || !preg_match("/^((bitcoincash:)?(q|p)[a-z0-9]{41})/",$value) || !preg_match("/^((BITCOINCASH:)?(Q|P)[A-Z0-9]{41})$/",$value))
                return $fail('BCH address entered is invalid. Please check the address and try again.');
            },
            'LTC-address' => 'regex:/^[LM3][a-km-zA-HJ-NP-Z1-9]{26,33}$/',

Please or to participate in this conversation.