laravel - 使用正则表达式允许字母字符、连字符、下划线、空格和数字

标签 laravel validation

我想在一个独特的情况下使用 Laravel 进行验证。我授权的字段是一本书的名字。所以它可以有字母字符、数字字符、空格和连字符/下划线/任何其他键。在输入任何键之前,我唯一不希望它有的是开头的空格。所以名称不能是“L”,注意空格,而“L L L”是完全可以接受的。在这种情况下,有人可以帮助我吗?

到目前为止,我得到了这样的正则表达式验证:

regex:[a-z{1}[A-Z]{1}[0-9]{1}]

我不确定如何包含其他限制。

最佳答案

  • 简答:

对于带空格的 alpha_num 使用此正则表达式:

'regex:/^[\s\w-]*$/'
  • 再长一点:)

这里是正则表达式的一些定义 block :

^           ==>  The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted
$           ==>  Same as with the circumflex symbol, the dollar sign marks the end of a search pattern
.           ==>  The period matches any single character
?           ==>  It will match the preceding pattern zero or one times
+           ==>  It will match the preceding pattern one or more times
*           ==>  It will match the preceding pattern zero or more times
|           ==>  Boolean OR
–           ==>  Matches a range of elements
()          ==>  Groups a different pattern elements together
[]          ==>  Matches any single character between the square brackets
{min, max}  ==>  It is used to match exact character counts
\d          ==>  Matches any single digit
\D          ==>  Matches any single non digit character
\w          ==>  Matches any alpha numeric character including underscore (_)
\W          ==>  Matches any non alpha numeric character excluding the underscore character
\s          ==>  Matches whitespace character

如果你想添加一些其他字符,你应该做的就是将它添加到 [] block 中。

例如,如果您想要允许 , ==> 'regex:/^[\s\w-,]*$/'

PS:如果你想要一个像我们这样的 setial 字符,还有一件事\* 或 .你必须像这样逃避它们\* 。

对于 * ==> 'regex:/^[\s\w-,\*]*$/'

关于laravel - 使用正则表达式允许字母字符、连字符、下划线、空格和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45352182/

相关文章:

javascript - 如何在提交表单之前立即对 radio 输入设置 CustomValidity

php - 将数据从表复制到表: validate mysql query

validation - 在 Yii 中执行 "massive assignment"时是否执行验证?

xml - 使用 `<xs:any>` 对嵌套元素进行不一致的 XSD 验证

Laravel 顺序处理用户的并发请求

laravel - Bootstrap 4 与 Laravel 5.7 一起安装

php - 如何在不访问 Laravel (PHP) 中的 Web 的情况下更新表格?

php - Laravel - 未找到命名空间中的类

laravel - Laravel 5.4 中的字段验证默认消息更改

java - 为列表中的特定元素设置 ValidationError 消息