Friday, October 29, 2021

How to Validate Money in Laravel request class?

 

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();

}


No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...