Laravel - 使用正则表达式仅验证字母、数字和空格

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

Laravel - Validate only letters, numbers and spaces using regex

regexvalidationlaravel

提问by gnardell

I am trying to validate a field made ??up of letters, numbers and spaces in this way:

我正在尝试以这种方式验证由字母、数字和空格组成的字段:

$rules = array(
    'field'=> 'regex:/([A-Za-z0-9 ])+/'
);

but if I try to insert characters like ' " and others, the validation still has to be successful.

但是如果我尝试插入像 '" 和其他字符这样的字符,验证仍然必须成功。

What's wrong 'in my regular expression?

我的正则表达式有什么问题?

回答by Cas Bloem

$rules = array(
    'field'=> 'regex:/(^[A-Za-z0-9 ]+$)+/'
);

回答by hovszabolcs

If you are working with utf-8

如果您使用的是 utf-8

$rules = array(
    'field'=> 'regex:/(^[\pL0-9 ]+$/u'
);