Home > Backend Development > PHP Tutorial > Can Laravel's Validation Class Validate Exact Values?

Can Laravel's Validation Class Validate Exact Values?

DDD
Release: 2024-11-14 17:41:02
Original
969 people have browsed it

Can Laravel's Validation Class Validate Exact Values?

Validating Exact Values with Laravel's Validation Class

Does Laravel's validation class allow for validating an input to match one or more exact values?

For example, consider the following validation rule:

$rules = [
    "field" => "hello"
];
Copy after login

In this case, we want to ensure that the value of the "field" input is exactly "hello".

Answer:

Yes, Laravel's validation class provides two options for validating exact values:

Option 1: Using the 'in' Rule

The 'in' rule takes a comma-separated list of acceptable values. For our example, the validation rule would be:

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

Option 2: Using a Regular Expression

A regular expression can also be used to match exact values. In our example, the regex would be:

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

The "^" and "$" delimiters ensure that the entire input value matches the regex.

Note that you may not need the "^$" delimiters in the regex, but it is worth testing to confirm.

The above is the detailed content of Can Laravel's Validation Class Validate Exact Values?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template