Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimum Age validation laravel

I want to validation a users information like date of birth for that i have used following line of statement in validation, But how do i make it dynamic so users minimum age could be 13. below that can't register?

  return Validator::make($data, [
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6',
            'date_of_birth' =>'required|date|before:2011-06-08',
        ]);
like image 997
User57 Avatar asked Jan 18 '26 05:01

User57


1 Answers

According to the docs at https://laravel.com/docs/5.5/validation, you should be able to use any valid string that works with the strtotime() function. In this case strtotime('13 years ago') is a valid descriptor for the dynamic date you are after. Hence, the following should work:

return Validator::make($data, [
    'first_name' => 'required|string|max:255',
    'last_name' => 'required|string|max:255',
    'email' => 'required|string|email|max:255|unique:users',
    'password' => 'required|string|min:6',
    'date_of_birth' =>'required|date|before:13 years ago',
]);
like image 79
Mark Menzies Avatar answered Jan 19 '26 18:01

Mark Menzies



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!