htmlparsley.js

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:


Solution

  • 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.