Validating Jquery Price Format Output Money

Asked

Viewed 82 times

0

I am using Jquery Price Format to format money using ,(vírgula)

to separate pennies and the .(ponto) to separate thousands, leaving something like this:

inserir a descrição da imagem aqui

How can I validate this by only accepting numbers, dots and commas?

Obs: the prefix code R$ is not sent.

2 answers

2

one of the solutions is to use a Pattern in the input field

follows an ex:

<input type="number" pattern="^[R$\-\s]*[\d\.]*?([\,]\d{0,2})?\s*$" />

this validates the following entries

1 => true
1,00 => true
R$1 => true
R$1000 => true
0,1 => true
1.000,00 => true
R$1.000.000 => true
5678 => true

and invalidates these other

1,001 => false
02,0 => false
22.42 => false
001 => false
192,168,1,2 => false
. => false
,55 => false
2000.000 => false

also you work with the mask of the field to force the use of this data.

  • But that way it would be just client side right? That’s easy to get around.

  • but dai vc would validate the rest in your php or language q vc uses from backend tbm comexpressao regular the way q i did vc so adapts pro preg_replace from php

  • That was the question

2


In PHP you can use a regex to validate the data:

$valid = preg_match('/^(\d{1,3}\.)*?(\d{1,3}),\d{2}$/','99.999,99');

The variable $valid will store the boolean value that will inform you if the value is correct (true), or if it is not correct (false).

Regular expressions

  • 1

    Thank you, I did some tests and it worked great:)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.