Home >Backend Development >PHP Tutorial >How to Validate Exact Matching Input Values in Laravel?

How to Validate Exact Matching Input Values in Laravel?

Susan Sarandon
Susan SarandonOriginal
2024-11-14 19:04:02994browse

How to Validate Exact Matching Input Values in Laravel?

Validate Exact Matching Input Values in Laravel

When validating input in Laravel, it is often necessary to ensure that a certain field matches an exact value. The validation class provides multiple options for performing this type of validation.

Option 1: Using 'in'

The 'in' rule can be used to validate that the input value is equal to one of a comma-separated list of acceptable values. For example, to require that the "field" field can only have the value "hello," the following rule can be used:

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

Option 2: Using Regular Expression

A regular expression can also be used to match the input value against a specific pattern. To require that the "field" field contain exactly the word "hello," the following rule can be used:

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

Additional Note

While the examples provided use the '^' and '$' delimiters in the regular expression, these delimiters may not be necessary depending on the specific regular expression used.

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

Statement:
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