user16725381
user16725381

Reputation:

Make custom validation for number in Laravel

I have a number filed, I need to make a custom validation for it like this

He can not write a number without put at the beginning of number one of these number

077 | 075 | 078 | the number must be like this 07701231231

Upvotes: 1

Views: 144

Answers (2)

Mahmood Ahmad
Mahmood Ahmad

Reputation: 492

There are many ways to do so, one of them (accepted answer) is to use starts_with validation, another one is to use regex validation, here is the code for the regex validation:

'number' => 'string|regex:/^(077|075|078)\d+$/'

Upvotes: 0

Tim Lewis
Tim Lewis

Reputation: 29257

I think the starts_with validation rule fits your needs here:

https://laravel.com/docs/8.x/validation#rule-starts-with

Since 077, 075 and 078 are not valid integer values (would convert to 77, 75, and 78), you'd use the following rule and handle that as a string:

'number' => 'string|starts_with:077,078,075'

Upvotes: 1

Related Questions