正则表达式验证字母、点和破折号 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37180170/
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-09-14 13:47:04  来源:igfitidea点击:

Regex validation letters, dots and dash Laravel

phpregexlaravel

提问by Sebastian Corneliu V?rlan

I have this validation:

我有这个验证:

'users.first_name' => 'required|min:2|regex:/[A-Za-z. -]/|max:255',

Why this validation pass this name: John[][]

为什么这个验证通过这个名字:John[][]

回答by revo

Someone in comments also pointed to this issue that by /[A-Za-z. -]/you don't care about all characters but saying that's enough for me if field under validation has only the least of that characters.

评论中的某人还指出了这个问题,/[A-Za-z. -]/您并不关心所有字符,但如果验证中的字段只有最少的字符,那么这对我来说就足够了。

To have only those characters you should specify beginning and ending of the input text by using a caret ^and $:

要仅包含这些字符,您应该使用插入符号^和指定输入文本的开头和结尾$

regex:/^[A-Za-z. -]+$/

回答by Abhishek

use Alpha dash validation

使用Alpha 破折号验证

'users.first_name' => 'required|min:2|alpha_dash|max:255',