Wednesday, February 28, 2018

php - Laravel regex validation for price OR empty




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})?)?$/'
];

Answer



is this work ?



$rules = [

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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...