Laravel 仅数字验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36637194/
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-09-14 13:37:01  来源:igfitidea点击:

Laravel Numbers Only Validation

validationlaravel

提问by otezz

How to validate only numbers in laravel? I've try numeric and digits but it accept + and - character as a valid numbers / digits which I don't want. (+123 or -123 is validated as valid numeric)

如何仅验证laravel中的数字?我尝试过数字和数字,但它接受 + 和 - 字符作为我不想要的有效数字/数字。(+123 或 -123 被验证为有效数字)

Is there a existing rule on laravel that only accept 0-9 as a valid numbers or I have to go with regex instead?

laravel 上是否存在仅接受 0-9 作为有效数字的现有规则,或者我必须改用正则表达式?

回答by otezz

So I end up using regex instead.

所以我最终使用了正则表达式。

regex:/^[0-9]+$/

回答by Nysso

try this rule :

试试这个规则:

digits_between:min,max

it only allows numbers, you just need to specify the min and max number of digits.
exemple:

它只允许数字,您只需要指定最小和最大位数。
例子:

'CowsNumber' => 'digits_between:3,10',