How to validate exact words in an input value in Laravel?

Validate exact words: This post will show you how to validate a string by exact matching with a string.

For example you have to validate a field for whether it has yes or no in an input. So the input value of the string should match with yes or no and nothing else, exactly.

Lets see how to match a string with exact word in Laravel with help of some examples.

Validate exact word

You can match a single word to a fixed string value. For example a field value should be earth and nothing else.

rules = [
    'field' => 'in:earth',

Validate exact words

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

The input value must have either sun or moon.

Note: in validator can be used to match of exact words and is case sensitive. For case sensitive word match you should see this post.


Leave a Reply