用逗号作为小数点分隔符验证数字的最佳方法是什么? [英] What's the best way to validate numbers with comma as decimal separator?

查看:138
本文介绍了用逗号作为小数点分隔符验证数字的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

什么是最好的方法:

  • 在验证之前是否遵守规则并用点替换逗号?是否有一个before_validation观察者或类似的东西?
  • 建立自定义验证规则?例如french_numeric?

推荐答案

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

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

$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.

这篇关于用逗号作为小数点分隔符验证数字的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆