How to Validate Money in Laravel request class?

10-Apr-2023

.

Admin

How to Validate Money in Laravel request class?

Hi dev,

In this blog,I will explain how to validate money in laravel request class.i can laravel money validation using regex.you can using regex format to Money validation in laravel we will easy following example to show laravel in money validation.

The regex will hold for quantities like '12' or '12.5' or '12.05'. If you want more decimal points than two, replace the "2" with the allowed decimals you need.so I will add 'amount'=>"required|regex:/^\d+(\.\d{1,2})?$/" or 'amount'=>"required|regex:/^\d{1,13}(\.\d{1,4})?$/" this line in my rules array , ANd will it work for both integer and float.

Laravel Money Validate Example


/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function store(Request $request){

$request->validate([

"product" =>" required",

"amount" =>" required|regex:/^\d+(\.\d{1,2})?$/",

]);

return redirect()->back();

}

or

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function store(Request $request){

$request->validate([

"product" =>" required",

"amount" =>" required|regex:/^\d{1,13}(\.\d{1,4})?$/",

]);

return redirect()->back();

}

it will help you....

#Laravel 7

#Laravel

#Laravel 6