Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel regex validation for price OR empty

Tags:

regex

php

laravel

I am trying to make a regex for price OR empty. I have the price part (Dutch uses comma instead of point) which actualy works

/^\d+(,\d{1,2})?$/

The regex above validates ok on the value 21,99

Now I try to add the empty part so the field can be... just empty ^$

/(^$|^\d+(,\d{1,2})?$)/

But Laravel starts to complain as soon as I change the regex: "Method [validate^\d+(,\d{1,2})?$)/] does not exist."

Works ok:

$rules = [
    'price' => 'regex:/^\d+(,\d{1,2})?$/'
];

Laravel says no...:

$rules = [
    'price' => 'regex:/(^$|^\d+(,\d{1,2})?$)/'
];

Kenken9990 answer - Laravel doesn't break anymore but an empty value is still wrong:

$rules = [
    'price' => 'regex:/^(\d+(,\d{1,2})?)?$/'
];
like image 648
poashoas Avatar asked Jan 21 '26 14:01

poashoas


1 Answers

is this work ?

$rules = [
    'price' => 'nullable|regex:/^(\d+(,\d{1,2})?)?$/'
];
like image 194
kenken9999 Avatar answered Jan 23 '26 06:01

kenken9999



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!