Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Validate year with regex validation in Laravel?

in Laravel application I need save year input data, it is should be numeric number and should include only 4 numbers. this is current system,

'year'    => 'required|min:4|numeric', 

this is working but can enter more than 4 numbers. how can enter only 4 numbers here?

like image 386
Muru Avatar asked Oct 18 '25 17:10

Muru


2 Answers

You can try

'year' => 'required|digits:4|integer|min:1900|max:'.(date('Y')+1),

the +1 is to account for a new year registration in Australia for example. You don't want to accidentally cut them off because they are ahead. Or, maybe more precise, use \Carbon\Carbon::tomorrow()->year

like image 83
Web Artisan Avatar answered Oct 21 '25 07:10

Web Artisan


For numeric data, value corresponds to a given integer value:

'year' => 'required|integer|min:1900|max:2018'
like image 25
IndianCoding Avatar answered Oct 21 '25 06:10

IndianCoding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!