Parsley.js - check optional input only for numbers

I have a form that has one additional input and 3 required input fields. For additional input, I have the following markup:

<input type="number" placeholder="0" min="0" max="20000" step="100" data-parsley-validation-threshold="1" data-parsley-trigger="keyup"> 

This does not trigger a check if I have a letter type or any other characters. It checks the minimum and maximum values. If I set the required attribute, it seems to work, but I don't want this. I also tried to define a template as shown below:

 data-parsley-pattern="^[0-9]*$" 

None of them seem to work. Any ideas?

+5
source share
1 answer

You can use data-parsley-type="digits" . Note the use of type="text" instead of number .

This works if you want to allow only numbers (not floating).

 <input type="text" placeholder="0" min="0" max="20000" step="100" data-parsley-validation-threshold="1" data-parsley-trigger="keyup" data-parsley-type="digits" /> 

If you want floats, you can use data-parsley-type='number' :

 <input type="text" placeholder="0" min="0" max="20000" step="100" data-parsley-validation-threshold="1" data-parsley-trigger="keyup" data-parsley-type="number" /> 
+14
source

Source: https://habr.com/ru/post/1208513/


All Articles