hello world
hello world

Reputation: 1755

laravel validation rule for only letters

I am trying to add a validation rule to only accept letters. I am using the regex rule but its still not working. Here is my code below:

    /**
     * Validate request/input 
     **/
    $this->validate($request, [
        'name' => 'required|regex:/^[\pL\s\-]+$/u|max:255|unique:users,name,'.$user->id,
        'email' => 'required|email|max:255|unique:users,email,'.$user->id,
    ]);

Whenever I enter a name like Foo Foo, it is still succesful in signing up.

Any idea what I am doing wrongly?

Upvotes: 20

Views: 85621

Answers (2)

Azimi
Azimi

Reputation: 9627

According to laravel documentation you can use :

  • alpha for entirely alphabetic characters
  • alpha_dash for alpha-numeric characters, as well as dashes and underscores and finally
  • alpha_num for entirely alpha-numeric characters.

Upvotes: 42

ExohJosh
ExohJosh

Reputation: 1892

Your regex fails I think, I tried it on Regexr and couldn't get it to function, try this:

'name' => 'required|regex:/^[a-zA-Z]+$/u|max:255|unique:users,name,'.$user->id,

Let me know how you get on.

Here's the regex only: ^[a-zA-Z]+$

Upvotes: 34

Related Questions