0

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?

1
  • 1
    What's wrong with the built-in alpha_num rule? Commented Jul 6, 2017 at 18:24

1 Answer 1

5

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'
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.