SheppardDigital
SheppardDigital

Reputation: 3255

Laravel input validation, alphanumeric but don't allow spaces

I have a field in a form which accepts a username. I need to use Laravels validation to validate the username but I'm struggling to come up with a solution which allows both alpha and numeric characters, but does not allow spaces in the username.

My Current validator looks like this;

$validatorFields = [
            'name' => 'required|string|max:150|regex:/^[A-Za-z0-9\-\s]+$/'
        ];

Can anyone suggest how to do this please?

Upvotes: 0

Views: 6965

Answers (1)

Leo
Leo

Reputation: 7420

You could use alpha_num The field under validation must be entirely alpha-numeric characters. Does not allow spaces as well. Example:

$validatorFields = [
  'name' => 'required|string|max:150|alpha_num'
];

Upvotes: 5

Related Questions