laravel 用逗号作为小数点分隔符验证数字的最佳方法是什么?

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

What's the best way to validate numbers with comma as decimal separator?

phpvalidationlaravel

提问by Samuel De Backer

In a Laravel app, I have a form on which I need to validate numbers with a comma as the decimal separator. For the moment, it only works with a point because my validation rule is:

在 Laravel 应用程序中,我有一个表单,我需要在表单上使用逗号作为小数点分隔符来验证数字。目前,它只适用于一点,因为我的验证规则是:

$rules = [
    'amount' => 'numeric|min:0',
];

What's the best method :

什么是最好的方法:

  • Keep the rule and replace comma with point before validation ? Is there a before_validation observer or something like this ?
  • Build a custom validation rule ? french_numeric for example ?
  • 保持规则并在验证前用点替换逗号?是否有 before_validation 观察者或类似的东西?
  • 建立自定义验证规则 ? 例如 french_numeric ?

回答by The Alpha

Laravel supports regexpattern in validation rule so you can use the given pattern to match something like 12,365.00and it's recommended to use an array instead of pipe when using regular expressions as a rule

Laravel 支持regex验证规则中的模式,因此您可以使用给定的模式来匹配类似的东西,12,365.00并且在使用正则表达式作为规则时,建议使用数组而不是管道

$rules = array('amount' => array('match:/^[0-9]{1,3}(,[0-9]{3})*\.[0-9]+$/'));

Check this link.Also, if you want to remove the commas for any reason then check this answer.

检查此链接。另外,如果您出于任何原因想删除逗号,请检查此答案

回答by Mario Haubenwallner

Building on the excellent answer from The Alpha, here is a code snippet to make a float validation configurable.

基于 The Alpha 的出色答案,这里有一个代码片段,用于使浮动验证可配置。

Add this snippet to the boot()function in your AppServiceProviderclass (tested with Laravel 5.4):

将此代码段添加到boot()您的AppServiceProvider类中的函数中(使用 Laravel 5.4 测试):

Validator::extend('float', function ($attribute, $value, $parameters, $validator) {
    $thousandsSeparator = env('APP_NUMBER_THOUSANDS_SEPARATOR') == '.' ? '\' . env('APP_NUMBER_THOUSANDS_SEPARATOR') : env('APP_NUMBER_THOUSANDS_SEPARATOR');
    $commaSeparator = env('APP_NUMBER_COMMA_SEPARATOR') == '.' ? '\' . env('APP_NUMBER_COMMA_SEPARATOR') : env('APP_NUMBER_COMMA_SEPARATOR');
    $regex = '~^[0-9]{1,3}(' . $thousandsSeparator . '[0-9]{3})*' . $commaSeparator . '[0-9]+$~';
    $validate = preg_match($regex, $value);

    if ($validate === 1) {
        return true;
    }

    return false;
});

Your .env-file would have those two lines:

你的 .env 文件会有这两行:

APP_NUMBER_COMMA_SEPARATOR="."
APP_NUMBER_THOUSANDS_SEPARATOR=","

And your rule would look like this:

您的规则如下所示:

$rules = [
    'amount' => 'float|min:0',
];

Note: I'm only escaping the .correctly. If you are going to use charaters that have special meaning in regex syntax (like * or +) you have to escape them too.

注意:我只是.正确地转义了。如果您要使用在正则表达式语法中具有特殊含义的字符(如 * 或 +),您也必须对它们进行转义。

But since a float numberlike 550*345,00 (550,345.00)or 57+44 (57.44)wouldn't make sense I've ignored this issue.

但是由于像或没有意义的浮点数我忽略了这个问题。550*345,00 (550,345.00)57+44 (57.44)

Kind regards

亲切的问候