Based on an excellent answer from Alpha, here is a code snippet to do a float validation.
Add this snippet to the boot() function in your AppServiceProvider class (tested with 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 will have two lines:
APP_NUMBER_COMMA_SEPARATOR="." APP_NUMBER_THOUSANDS_SEPARATOR=","
And your rule will look like this:
$rules = [ 'amount' => 'float|min:0', ];
Note. I'm just running away . right. If you intend to use charaters that have special meaning in regex syntax (e.g. * or +), you should also avoid them.
But since a floating point number , for example 550*345,00 (550,345.00) or 57+44 (57.44) , does not make sense, I ignored this problem.
Yours faithfully
source share