哪个验证规则用于 Laravel 4 的浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22187615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which validation rule to use for a float with Laravel 4?
提问by Isaias
I have a form with two text inputs that must be floats (specifically, these text inputs are for geographic coordinates
), looked at the documentationand found a rule for integer
and numeric
but not for float
.
我有一个包含两个文本输入的表单,它们必须是浮点数(具体来说,这些文本输入是 for geographic coordinates
),查看了文档并找到了一个规则 for integer
andnumeric
但不是 for float
。
I was thinking using the "numeric" because the text inputs are disabled and the value only changes when a marker on a map is moved.
我正在考虑使用“数字”,因为文本输入被禁用,并且只有在移动地图上的标记时才会更改值。
What would be the best way to validate a float?
验证浮动的最佳方法是什么?
采纳答案by The Alpha
You may use a regular expression rule (regex:pattern
) for this and since you want to use to validate Geographic Coordinates
then you should use a regular expression rule because a Geo Coord
may look something like 23.710085, 90.406966
, which is the coordinates (lat long) of Dhaka Bangladesh
and it also may contain a coordinates like -33.805789,151.002060
. So here is the syntax:
您可以regex:pattern
为此使用正则表达式规则 ( ) 并且由于您要用于验证,Geographic Coordinates
因此您应该使用正则表达式规则,因为 aGeo Coord
可能看起来像23.710085, 90.406966
,它是 的坐标(经纬度),Dhaka Bangladesh
它也可能包含坐标喜欢-33.805789,151.002060
。所以这里是语法:
$rules = array('form_field_name' => 'required|regex:pattern' );
Or maybe just
或者也许只是
$rules = array('form_field_name' => 'regex:pattern' );
So, the pattern should be something like this /^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$/
. So, finally, it should look something like this (pattern is copied from internet
):
所以,模式应该是这样的/^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$/
。所以,最后,它应该看起来像这样 ( pattern is copied from internet
):
$rules = array('form_field_name' => 'regex:/^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$/');
Check the Laravel Validation (regex).
回答by Kemal Fadillah
The numeric
rule can be used for that because it uses the is_numeric()
function to test the value. Although, normally you'd want something that uses the is_float()
function, since form inputs are in the form or string, is_numeric()
should be used instead. Here's a quote from the PHP doc for is_float:
该numeric
规则可用于此,因为它使用is_numeric()
函数来测试值。虽然,通常你想要使用该is_float()
函数的东西,因为表单输入是表单或字符串,is_numeric()
应该使用。这是PHP 文档中 is_float的引用:
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
要测试变量是数字还是数字字符串(例如表单输入,它始终是字符串),必须使用 is_numeric()。
回答by Raul Duran
'latitude' => 'regex:/^-?\d{1,2}\.\d{6,}$/',
'longitude' => 'regex:/^-?\d{1,2}\.\d{6,}$/',
回答by macieks
Validator::extend('gps', function($attribute, $value, $parameters) {
if (preg_match('/^-?\d{1,2}\.\d{6,}\s*,\s*-?\d{1,2}\.\d{6,}$/', $value)) {
return true;
} else {
return false;
}
});