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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:56:40  来源:igfitidea点击:

validating a numeric input's length in laravel 5

phplaravellaravel-5laravel-5.2laravel-validation

提问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-idfield should contain 10digitsand I actually expected the code above to validate this , but instead It will check If the national-idexactly equals to 10 or not ...

how can I validate the lengthof a numeric field?

national-id字段应该包含10digits并且我实际上希望上面的代码来验证这一点,但它会检查是否national-id完全等于 10 或不......

我如何验证length数字字段的?

回答by Marcin Nabia?ek

In fact you don't need to use digits_betweenrule. You can use digits ruleso according to documentation it will be enough to use:

事实上,您不需要使用digits_between规则。您可以使用数字规则,因此根据文档,使用它就足够了:

$rules = [
    'national-id' => 'required|digits:10'
];

without numericrule because digitsrule 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'