简体   繁体   中英

Parsley.js - Validate optional input for only numbers

I have a form that has one optional input and 3 required input fields. For the optional input I have the below markup:

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

This does not fire the validation if I have type in letters or any other characters. It does validate for min and max values. If I put required attribute it does seem to work but I don't want that. I also tried defining a pattern as below:

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

None of them seem to work. Any ideas?

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

This works if you only want to allow digits (not floats).

<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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM