Laravel validate decimal 0-99.99

让人想犯罪 __ 提交于 2019-12-18 12:49:39

问题


Laravel does not have a validation for decimal, so I need a regex or other validation method to validate a numeric value of 0 - 99.99

I have tried

required|regex:^\d{0,2}(\.\d{1,2})?$/
required|regex:[1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9]
required|regex:/[1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9]/
required|regex:/[\d]{2}.[\d]{2}/
required|regex:/[\d]{0-2}.[\d]{0,2}/

I am either getting "invalid format" when trying to enter 0.05 or unknown modifiers for preg_match

any help with the regex needed to validate a decimal in laravel is appreciated (with optional values before or after decimal


回答1:


The Laravel between validation rule is actually pretty powerful and can handle decimal values as well. So there's no need to use regex just do this:

'required|between:0,99.99'



回答2:


The laravel(5?) between rule does always get it has to parse a decimal, you have to add numeric to it to validate correctly

'required|numeric|between:0,99.99',



回答3:


this will work for number allowing two decimal precision:

function rules(){

   return [
   'field_name'=> array('regex:/^(?:d*.d{1,2}|d+)$/','min:1','max:10')
        ]; 
}


来源:https://stackoverflow.com/questions/29734046/laravel-validate-decimal-0-99-99

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!