fightstarr20
fightstarr20

Reputation: 12568

Laravel 5.6 - Validate Numbers and spaces

In Laravel 5.6 I am validating alphabetic characters and spaces only in regex like this..

'name' => 'required|regex:/^[\pL\s\-]+$/u',

This works as far as I can see, I am now trying to validate numbers only and spaces like this..

'telephone' => 'required|regex:/[0-9 ]+/',

But this is not working and allows me to enter 'f4' where it should fail.

Where am I going wrong?

Upvotes: 0

Views: 997

Answers (1)

The fourth bird
The fourth bird

Reputation: 163217

[0-9 ]+ will match 4 in f4

Try using anchors to assert the start and the end of the line ^ and $

^[0-9 ]+$

Note that this will also match whitespaces only because the character class matches digit or a whitespace one or more times..

Upvotes: 4

Related Questions