gnardell
gnardell

Reputation: 321

Laravel - Validate only letters, numbers and spaces using regex

I am trying to validate a field made ​​up of letters, numbers and spaces in this way:

$rules = array(
    'field'=> 'regex:/([A-Za-z0-9 ])+/'
);

but if I try to insert characters like ' " and others, the validation still has to be successful.

What's wrong 'in my regular expression?

Upvotes: 2

Views: 25580

Answers (2)

hovszabolcs
hovszabolcs

Reputation: 101

If you are working with utf-8

$rules = array(
    'field'=> 'regex:/(^[\pL0-9 ]+$/u'
);

Upvotes: 3

Cas Bloem
Cas Bloem

Reputation: 5040

$rules = array(
    'field'=> 'regex:/(^[A-Za-z0-9 ]+$)+/'
);

Upvotes: 18

Related Questions