php 在laravel 5中验证数字输入的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39539436/
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
validating a numeric input's length in laravel 5
提问by bobD
foo.blade.php
foo.blade.php
<input type="text" name="national-id" />
FooController.php
FooController.php
$rules = [
'national-id' => 'required|size:10|numeric'
];
the national-id
field should contain 10
digits
and I actually expected the code above to validate this , but instead It will check If the national-id
exactly equals to 10 or not ...
how can I validate the length
of a numeric field?
该national-id
字段应该包含10
digits
并且我实际上希望上面的代码来验证这一点,但它会检查是否national-id
完全等于 10 或不......
我如何验证length
数字字段的?
回答by Marcin Nabia?ek
In fact you don't need to use digits_between
rule. You can use digits ruleso according to documentation it will be enough to use:
事实上,您不需要使用digits_between
规则。您可以使用数字规则,因此根据文档,使用它就足够了:
$rules = [
'national-id' => 'required|digits:10'
];
without numeric
rule because digits
rule also verify if given value is numeric.
没有numeric
规则,因为digits
规则还验证给定值是否为数字。
回答by Carlos
When using size on a number it will check if the number is equal to the size, on string, it will check the amount of characters, for number you should use :
在数字上使用 size 时,它会检查数字是否等于大小,在字符串上,它将检查字符数,对于数字,您应该使用:
'number' => 'required|numeric|digits_between:1,10'
回答by Ray
In laravel the size validation on a field that's numeric will indicate the max integer it cannot be larger than.
在 Laravel 中,数字字段的大小验证将指示它不能大于的最大整数。
Use digits_between:
使用digits_between:
'national-id' => 'required|digits_between:10,10|numeric'