In Laravel, what verification rules should I check for the number of characters in a number?

I use rules in validation in Laravel and try to check the number of characters in a number.

This is my attempt:

protected static $rules = [
    'zip_code' => 'required|size:5|integer'
];

Unfortunately, this checks that the zip code is the number 5, not the 5-character zip code. What rules do I need to set in order to verify that the zip code is exactly 5 characters, regardless of the actual numbers?

+4
source share
3 answers

5 (- ). "" Laravel , :

protected static $rules = [
     'zip_code' => 'required|regex:/\b\d{5}\b/'
];

, 5 . Laravel Docs

+7

Laravel 5.3:

, , , 5 , :

protected static $rules = [
    'zip_code' => 'required|max:5|string'
];

, 5 , :

protected static $rules = [
    'zip_code' => 'required|max:99999|integer'
];

, 5 , :

protected static $rules = [
    'zip_code' => 'required|digits:5|integer'
];

zipcodes , zipcodes preg_match . AppServiceProvider boot. . :

https://laravel.com/docs/5.3/validation#custom-validation-rules

varchar (10) (, 10 laravel) integer zipcodes, :

  • MySQL, varchar.

  • 10 (zip + 4, : 12345-1234).

,

+1

: 5 try min: 5 | max: 5

0
source

All Articles