parsley.js

How handle decimal and thousands separators for numbers in Parsley.js?


I can see that Parsley.js handle English separators for numbers.

When I use Parsley with Spanish numbers it doesn't work (example : 5,50 is not a valid number for Parsley and in my country comma is our decimal separator).

Is there some config to change it?


Solution

  • You need to create the necessary regex that matches your needs. A (probably rudimentary) regex would be ^\d+(,\d+)?$, that matches any digits or digits with commas.

    You don't even need to create a custom validator, since Parsley allows you to use data-parsley-pattern which receives a regex.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.0.7/parsley.min.js"></script>
    <form data-parsley-validate>
      <input type="text" name="commaNumbers" data-parsley-pattern="^\d+(,\d+)?$" />
      <input type="submit" />
    </form>