How to Validate Exact Values in Laravel Inputs?

Susan Sarandon
Release: 2024-11-15 02:43:02
Original
998 people have browsed it

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',
];
Copy after login

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$',
];
Copy after login

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.

The above is the detailed content of How to Validate Exact Values in Laravel Inputs?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template