在 Laravel 中,我需要什么验证规则来检查数字中的字符数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28443847/
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
In Laravel, what validation rules do I need to check the number of characters in a number?
提问by zeckdude
I'm using rules in validation in Laravel, and am trying to check the number of characters in a number.
我在 Laravel 中使用验证规则,并试图检查数字中的字符数。
This is my attempt:
这是我的尝试:
protected static $rules = [
'zip_code' => 'required|size:5|integer'
];
Unfortunately that checks that the zip code is the number 5 and not that the zip code is 5 characters. Which rules do I need to set to check that the zip code is exactly 5 characters, regardless of the actual numbers?
不幸的是,检查邮政编码是数字 5 而不是邮政编码是 5 个字符。无论实际数字如何,我都需要设置哪些规则来检查邮政编码是否恰好为 5 个字符?
回答by Bogdan
A zip code is 5 digits long and may contain leading zeros (Northeast US). The Laravel "digits" check drops the leading zeros as a result you need to use a regex pattern:
邮政编码为 5 位数字,可能包含前导零(美国东北部)。Laravel“数字”检查删除前导零,因此您需要使用正则表达式模式:
protected static $rules = [
'zip_code' => 'required|regex:/\b\d{5}\b/'
];
This will make sure the zip code is always 5 digits long. More details in the Laravel Docs
这将确保邮政编码始终为 5 位数字。Laravel 文档中的更多详细信息
回答by Learner
As per Laravel 5.3 :
根据 Laravel 5.3 :
If you are validating against character fieldwhich contains a number and you wanna know if it max 5 or lesser digitsthen you can use:
如果您正在验证包含数字的字符字段,并且您想知道它是否最多 5 个或更少的数字,那么您可以使用:
protected static $rules = [
'zip_code' => 'required|max:5|string'
];
If you are validating against integer fieldand you wanna know if it contains max 5 or lesser digitsthen use:
如果您正在针对整数字段进行验证并且您想知道它是否包含最多 5 个或更少的数字,请使用:
protected static $rules = [
'zip_code' => 'required|max:99999|integer'
];
If you are validating against integer fieldand you wanna know if it contains exact 5 digitsthen you can use:
如果您正在针对整数字段进行验证并且您想知道它是否包含精确的 5 位数字,那么您可以使用:
protected static $rules = [
'zip_code' => 'required|digits:5|integer'
];
Although in last case, zipcodes with leading zeroes will be trimmed and that's why for zipcodes I recommend using preg_matchfor validation. Define a custom validation rule in your AppServiceProviderfile under bootfunction. For reference see this:
尽管在最后一种情况下,带有前导零的邮政编码将被修剪,这就是我建议使用preg_match进行验证的邮政编码的原因。在启动功能下的AppServiceProvider文件中定义自定义验证规则。供参考,请参阅:
https://laravel.com/docs/5.3/validation#custom-validation-rules
https://laravel.com/docs/5.3/validation#custom-validation-rules
and you should use varchar(10)(string,10 in laravel)instead of integer for zipcodes because:
并且您应该使用varchar(10)(string,10 in laravel)而不是整数作为邮政编码,因为:
You cannot define size of integer in MySQL but you can for varchar.
Zip codes can be 10 characters long with extension(zip+4, ex: 12345-1234).
你不能在 MySQL 中定义整数的大小,但你可以为 varchar 定义。
邮政编码可以是 10 个字符长并带有扩展名(zip+4,例如:12345-1234)。
I hope it helps
我希望它有帮助
回答by Rohan Mehta
You getting this wrong instead on size:5 try min:5|max:5
你在 size:5 上弄错了,试试 min:5|max:5