'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:
- 2.888 -> fails
- 2.8 -> fails
- 2 -> fails
- 2.88 -> validates
Solution 1:[1]
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
Solution 2:[2]
From java script answer for decimal validation
data-parsley-pattern="^\d+(.\d{1,2})?$"
also, please check if input type="text"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kaushik Tadhani |
| Solution 2 |
