Laravel自定义验证规则不允许仅允许标点符号或字符串中的任何表情符号

发布于 2025-02-13 09:07:15 字数 357 浏览 0 评论 0 原文

我对Regex并不擅长,并且想知道是否有人可以帮助解释如何不允许以下内容。我想阻止用户能够在字符串中输入任何表情符号以及只有标点符号。即

".."        => fail
"-----"     => fail
",,,,"      => fail
"Mc-Donald" => pass

字符串中的任何表情符号也应导致失败。

我正在考虑使用Regex为此设置自定义Laravel规则,以检查字符串是否仅包含标点符号或任何表情符号。

也许是一种更好的方法,即正则是只允许某些字符?因为我真的只想只允许字母,数字和标点符号(不仅是刺耳的刺耳),

感谢您的任何帮助!

I'm not great with RegEx and wondering if someone could help explain how I can go about not allowing the following. I want to block the user from being able to enter any emojis in a string as well as only punctuation. i.e.

".."        => fail
"-----"     => fail
",,,,"      => fail
"Mc-Donald" => pass

Any emojis in the string should also result in a fail.

I was thinking of setting up a custom Laravel rule for this using regex to check if the string contains only punctuation or any emojis.

Perhaps a better way might be for a RegEx to only allow certain characters? As I only really want to allow only letters, numbers and punctuation (just not only puncation on it's own)

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-02-20 09:07:15

查看验证规则 alpha_dash alpha_num 。如果您想对其进行Finetune,则可以使用以下代码自定义您自己的正则表达式:(

将其添加到服务提供商),

Validator::extend('alpha_num_dash', function($attribute, $value) {
    if(!is_string($value) && !is_numeric($value)) {
        return false;
    }
    return preg_match('/^[a-zA-Z0-9\-]+$/', $value);
});

我不会在此处解释这句话,因为它不超出范围。查找指南,您将很快学习其中的绳索。此外,您可以尝试使用用正则纠正。

Check out the validation rule alpha_dash https://laravel.com/docs/9.x/validation#rule-alpha-dash or alpha_num. If you want to finetune it, you can use the following code to customize your own regex:

(Add this to a service provider)

Validator::extend('alpha_num_dash', function($attribute, $value) {
    if(!is_string($value) && !is_numeric($value)) {
        return false;
    }
    return preg_match('/^[a-zA-Z0-9\-]+$/', $value);
});

I will not explain the regex meaning here since it is out of scope. Look up a guide and you will quickly learn the ropes of that. Furthermore, you could try an online service like https://regex101.com/r/PxtN3U/1 to tinker with the regex.

温柔少女心 2025-02-20 09:07:15

有一个php函数 ctype_punct_punct 字符不是空格或字母数字字符”。

类似的事情:

$validator = Validator::make($request->all(), [
    'title' => [
        'required',
        'max:255',
        function ($attribute, $value, $fail) {
            if (ctype_punct($value)) {
                $fail('The '.$attribute.' is invalid.');
            }
        },
    ],
]);

更详细的验证将需要正则态度,例如,必须包含至少一个“字母顺序的字符”:

$validator = Validator::make($request->all(), [
    'title' => ['required','max:255','regex:/[a-zA-Z]/'],
]);

There is a php function ctype_punct that "check for any printable character which is not whitespace or an alphanumeric character".

Something like that:

$validator = Validator::make($request->all(), [
    'title' => [
        'required',
        'max:255',
        function ($attribute, $value, $fail) {
            if (ctype_punct($value)) {
                $fail('The '.$attribute.' is invalid.');
            }
        },
    ],
]);

More detailed validation will require regex, e.g. must contains at least one "alphabetical character":

$validator = Validator::make($request->all(), [
    'title' => ['required','max:255','regex:/[a-zA-Z]/'],
]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文