Php Laravel非零验证

Php Laravel非零验证,php,validation,laravel,Php,Validation,Laravel,我用的是拉威尔验证系统。我有一个字段是数据库,作为批发价格,价格是十进制的。为了验证,我用这个 'wholesale_price' => 'required|regex:/^\d*(\.\d{1,2})?$/', 但价格不能是0。我必须验证>0 我该怎么做呢。在laravel min:1功能中,但这对我没有用处。因为我的价格是0.005或0.02,那么这个正则表达式呢: /^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/ 说明: ^ #

我用的是拉威尔验证系统。我有一个字段是数据库,作为批发价格,价格是十进制的。为了验证,我用这个

'wholesale_price' => 'required|regex:/^\d*(\.\d{1,2})?$/',
但价格不能是0。我必须验证>0

我该怎么做呢。在laravel min:1功能中,但这对我没有用处。因为我的价格是0.005或0.02,那么这个正则表达式呢:

/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/
说明:

^            # Start of string
\s*          # Optional whitespace
(?=.*[1-9])  # Assert that at least one digit > 0 is present in the string
\d*          # integer part (optional)
(?:          # decimal part:
 \.          # dot
 \d{1,2}     # plus one or two decimal digits
)?           # (optional)
\s*          # Optional whitespace
$            # End of string
结论:

'wholesale_price' => 'required|regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
这个正则表达式怎么样:

/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/
说明:

^            # Start of string
\s*          # Optional whitespace
(?=.*[1-9])  # Assert that at least one digit > 0 is present in the string
\d*          # integer part (optional)
(?:          # decimal part:
 \.          # dot
 \d{1,2}     # plus one or two decimal digits
)?           # (optional)
\s*          # Optional whitespace
$            # End of string
结论:

'wholesale_price' => 'required|regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',

您不必执行
regex
来解决这个问题。以以下测试为例:

$input = ["wholesale_price" => 0.005];
$rules = ["wholesale_price" => "numeric|between:0.001,99.99"];
只要您需要
数值
规则,那么
之间
将把正在验证的值视为一个数字(
int
浮点
双精度
,等等),只要您没有传递字符串值,例如
$0.001
,或者在验证之前去掉任何不需要的字符,对于高于
0
的任何值以及您设置的最大值(当前
99.99
,但您可以将其设置为您想要的最高值),此方法将返回true

下面是一个简单的测试模板:

$input = [
    "price" => 0
];
$input2 = [
    "price" => 0.001
];
$rules = [
    "price" => "numeric|between:0.001,99.99",
];

$validator = \Validator::make($input, $rules);
$validator2 = \Validator::make($input2, $rules);

dd($validator->passes());
// Returns false;

dd($validator2->passes());
// Returns true;
注意:如果
price
是一个字符串值,也可以使用,如果要将
$
发送到服务器,只需去掉它即可


希望有帮助

您不必执行
regex
来解决这个问题。以以下测试为例:

$input = ["wholesale_price" => 0.005];
$rules = ["wholesale_price" => "numeric|between:0.001,99.99"];
只要您需要
数值
规则,那么
之间
将把正在验证的值视为一个数字(
int
浮点
双精度
,等等),只要您没有传递字符串值,例如
$0.001
,或者在验证之前去掉任何不需要的字符,对于高于
0
的任何值以及您设置的最大值(当前
99.99
,但您可以将其设置为您想要的最高值),此方法将返回true

下面是一个简单的测试模板:

$input = [
    "price" => 0
];
$input2 = [
    "price" => 0.001
];
$rules = [
    "price" => "numeric|between:0.001,99.99",
];

$validator = \Validator::make($input, $rules);
$validator2 = \Validator::make($input2, $rules);

dd($validator->passes());
// Returns false;

dd($validator2->passes());
// Returns true;
注意:如果
price
是一个字符串值,也可以使用,如果要将
$
发送到服务器,只需去掉它即可


希望有帮助

使用自定义验证器。使用自定义验证器。谢谢:P偶然发现了为什么需要
数字规则的原因;如果没有,则
between
将该值视为
字符串,并根据规则检查其
strlen()
值,因此
strlen(“0.001”)
5
,介于
0.001和99.99
之间,因此总是通过验证。谢谢:P偶然发现了需要
数字规则的原因;如果没有,则
between
将该值视为
字符串,并根据规则检查其
strlen()
值,因此
strlen(“0.001”)
5
,介于
0.001和99.99
之间,因此始终通过验证。