php 仅用于字母的 Laravel 验证规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38598532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:38:51  来源:igfitidea点击:

laravel validation rule for only letters

phpregexlaravel

提问by hello world

Hey guys im trying to add a validation rule to only accept letters. Im using the regexrule but its still not working. Here is my code below

嘿伙计们,我正在尝试添加验证规则以仅接受字母。我正在使用该regex规则,但它仍然无法正常工作。下面是我的代码

    /**
     * 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 Fooits still succesful in signing up.

每当我输入一个名字时,Foo Foo它仍然会成功注册。

Any idea what im doing wrong?

知道我做错了什么吗?

回答by ExohJosh

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

我认为您的正则表达式失败了,我在Regexr上尝试过但无法使其正常运行,请尝试以下操作:

'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]+$

这里只有正则表达式:^[a-zA-Z]+$

回答by Hassan Azimi

According to laravel documentationyou can use :

根据laravel文档,您可以使用:

  • alphafor entirely alphabetic characters
  • alpha_dashfor alpha-numeric characters, as well as dashes and underscores and finally
  • alpha_numfor entirely alpha-numeric characters.
  • alpha对于完全字母字符
  • alpha_dash对于字母数字字符,以及破折号和下划线,最后
  • alpha_num对于完全字母数字字符。