如何验证带小数部分的货币格式。
我的请求将类似于12,133.00。嗯,你试过了,但到目前为止还不是很好。我是个初学者。
public function store(Request $request)
{
$this->validate($request, [
'price'=>'required|numeric',
]);
}嗯,我没抱太大期望。
public function store(Request $request)
{
$this->validate($request, [
'price'=>'required|numeric',
]);
$product->price=Input::get('price');
}发布于 2019-04-12 22:11:22
您想要做的是使用原生php number_format()函数。此处链接php.net中的文档。您想要做的事情如下:
$product = new Product;
$product->price = number_format($request->price, 2, ".", ",");您甚至可以将其简化为:
$product->price = number_format($request->price, 2);希望这能有所帮助。
发布于 2019-04-13 00:12:57
您可以利用正则表达式并尝试如下所示
$this->validate($request, [
'price'=>"required|regex:/^[0-9]{1,3}(,[0-9]{3})*\.[0-9]{0,2}+$/",
]);这将检查千位和小数。
应该适用于1.00,10.00,100.00,1000.00等,但不会验证缺少逗号的1000.00。
让我知道这是否适用于您的预期。
https://stackoverflow.com/questions/55653261
复制相似问题