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
laravel validation rule for only letters
提问by hello world
Hey guys im trying to add a validation rule to only accept letters. Im using the regex
rule 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 Foo
its 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文档,您可以使用:
alpha
for entirely alphabetic charactersalpha_dash
for alpha-numeric characters, as well as dashes and underscores and finallyalpha_num
for entirely alpha-numeric characters.
alpha
对于完全字母字符alpha_dash
对于字母数字字符,以及破折号和下划线,最后alpha_num
对于完全字母数字字符。