Javascript Parsley.js - 仅验证数字的可选输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27337618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsley.js - Validate optional input for only numbers
提问by user3312508
I have a form that has one optional input and 3 required input fields. For the optional input I have the below markup:
我有一个表单,其中包含一个可选输入和 3 个必填输入字段。对于可选输入,我有以下标记:
<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:
如果我输入了字母或任何其他字符,这不会触发验证。它确实验证了最小值和最大值。如果我放置 required 属性,它似乎确实有效,但我不想要那样。我还尝试定义一个模式如下:
data-parsley-pattern="^[0-9]*$"
None of them seem to work. Any ideas?
它们似乎都不起作用。有任何想法吗?
回答by Luís Cruz
You can use data-parsley-type="digits". Notice the use of type="text"instead of number.
您可以使用data-parsley-type="digits". 注意使用type="text"代替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':
如果你想要浮动,你可以使用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" />

