How to Validate Money in Laravel request class?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
How to Validate Money in Laravel request class

Hi dev’s, Today now in this blog,I will explain with you how to validate money in laravel request class. Here we can do laravel money validation by using regex. So we can do by using regex format to Money validation in laravel. So we can do easy by following example to show laravel in money validation.

By using the regex will hold for quantities as like as ‘8’ or ‘92.5’ or ‘82.05’. So if we want more decimal points than two, replace the “2” also with the allowed decimals we need. So I will add ‘amount’=>”required|regex:/^\d+(\.\d{1,2})?$/” or ‘amount’=>”required|regex:/^\d{1,13}(\.\d{1,4})?$/” on in 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();
}

Read Also : Laravel Ajax Image Upload With Intervention Package

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →