Vladimir-G
Vladimir-G

Reputation: 21

number format validation rule for laravel

In validation rule i have regex

'amount' => 'required|regex:/^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)*$/|not_in:0',

With this, accept ony 1,000,000.00

I need to accept for example all this formats 1000; 10000.00; 10,000.00

for examples: 1000 accept 1000.00 accept 10000000.00 accept 1000.0000 failed 1,000.00 accept 1,000.0000 failed 1,000,000.00 accept

Upvotes: 2

Views: 925

Answers (2)

Js Lim
Js Lim

Reputation: 3675

I would suggest you use numeric validation, after validate only use number_format

'amount' => 'required|numeric|not_in:0',

then

$amount = number_format($request->get('amount'), 2);

Simple, easy to understand

Upvotes: 0

Pavel
Pavel

Reputation: 936

Try this regexp(all your numbers in the post will be matched):

'amount' => 'required|regex:/^[0-9\.,]+$/|not_in:0'

Upvotes: 1

Related Questions