Juan Girini
Juan Girini

Reputation: 1168

How to validate decimal positions with Parsley or HTML5?

How can I validate an input to have an exact amount of decimals? For instance, if I want to validate a number to have 2 decimals then I want to validate as follow:

Upvotes: 3

Views: 8865

Answers (3)

Hardik
Hardik

Reputation: 91

From java script answer for decimal validation

data-parsley-pattern="^\d+(.\d{1,2})?$"

also, please check if input type="text"

Upvotes: 0

Kaushik Tadhani
Kaushik Tadhani

Reputation: 11

You could use the data-parsley-pattern attribute, and use a regular expression to match numbers to 2 decimal places or zero.

^[0-9]{1,}\.?[0-9]{0,2}$

for example:

  • If the value is 1.00 then it will valid
  • If the value is 123.00 then it will valid
  • If the value is .00 then it will not valid
  • If the value is 1.000 then it will not valid

Upvotes: 1

John Bupit
John Bupit

Reputation: 10618

You could use the data-parsley-pattern attribute, and use a regular expression to match number to 2 decimal places:

data-parsley-pattern="^[0-9]*\.[0-9]{2}$"

Also, remember to add a required attribute to your tag.

Upvotes: 13

Related Questions