Laravel Validation - number must start with 0 (zero)

。_饼干妹妹 提交于 2019-12-08 03:33:01

问题


How to do input validation that a number must start with 0?

For example:

$rules = [
    'full_name' => 'required|min:6',
    'number' => 'required|numeric|min:5',
];

On the number field, it is requred, must be numberic only and minimum of 5 digits. I also need to include rule that it must start with 0?


回答1:


Use laravel regex:pattern validator, inside an array (Issue with Laravel Rules & Regex (OR) operator) and a regex of something like this: /^[0][0-9]{4,}/




回答2:


You can try it with

substr(Input::get('number'), 0, 1);

as data field and the rule

'in:0'

But as php will cut leading zeros if you are transmitting the number as int, it will most likely be hard. If it's a number as string, it will work.



来源:https://stackoverflow.com/questions/28094604/laravel-validation-number-must-start-with-0-zero

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