首页  >  文章  >  后端开发  >  如何验证 Laravel 输入中的精确值?

如何验证 Laravel 输入中的精确值?

Susan Sarandon
Susan Sarandon原创
2024-11-15 02:43:021002浏览

How to Validate Exact Values in Laravel Inputs?

Validating Exact Values in Laravel Inputs

In Laravel's validation system, you can enforce strict matching of input values against specified exact values. This can be useful when you need to ensure that users input specific predefined words or phrases.

Validation Option 1: 'in' Rule

The 'in' validation rule allows you to define a comma-separated list of acceptable values. For instance, to validate that the 'field' input must be "hello," you would use the following rule:

$rules = [
    'field' => 'in:hello',
];

Validation Option 2: Regular Expression Rule

Alternatively, you can use a regular expression to validate exact values. To match a specific word, you can use the following regular expression:

$rules = [
    'field' => 'regex:^hello$',
];

In this case, the ^ and $ delimiters ensure that the entire input matches "hello" exactly. Note that you may need to adjust the flag delimiters depending on your regex engine.

以上是如何验证 Laravel 输入中的精确值?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn