https://laravel.com/docs/5.8/validation#rule-regex
'field' => 'regex:/^[a-z0-9\s]+$/i',
// or
'field' => 'regex:/^[a-z0-9\s]*$/i',
Use + (first version) for one or more, and the * (second) for zero or more.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a form config field that needs to validate if it is a number or string (potentially with spaces).
How can I do this?
I was thinking alpha_num but I don't think that will work with spaces, etc.
Is there a way to check for string OR numeric? Custom rule? What do you think?
Can I just do string|numeric?
Sorry, testing this now, but curious if anyone has a thought on how to tackle this.
Thanks for the responses, I actually wound up not needing this but went for this approach at first...
$rules = [
'thing1' => 'required'
];
if (gettype(request()->thing2 === 'numeric')) {
$rules['thing2'] => 'numeric';
} else {
$rules['thing2'] => 'string';
}
return $rules;
Please or to participate in this conversation.