[英]How to validate an interval entered to a text input field
Is there a way to define a number interval (let's say between 1900 and 2016) in jQuery that can only be entered to a text input field? 有没有一种方法可以在jQuery中定义一个只能输入文本输入字段的数字间隔(假设在1900年至2016年之间)?
As far as I understand, I have to use input type="text" because the number field can't be used for a jQuery keyboard. 据我了解,我必须使用输入type =“ text”,因为number字段不能用于jQuery键盘。 In addition, it is required that values be entered both by a virtual and real keyboard (see http://generace.forbes.cz/ ) and that the number interval be checked each time user tries to submit the form. 另外,还要求同时使用虚拟键盘和真实键盘输入值(请参阅http://generace.forbes.cz/ ),并要求每次用户尝试提交表单时都检查数字间隔。
This is the far most I can get to without doing any dirty work. 这是我不做任何肮脏工作所能获得的最大成果。
https://jsfiddle.net/mankinchi/uupytLh2/ https://jsfiddle.net/mankinchi/uupytLh2/
pattern="\d{4}"
if ((number < 1900) || (number > 2016))
This use the combination of having a pattern to check (4 digits only when submitting) and javascript (I used jQUery) to check for number range. 这结合了使用模式检查(提交时仅4位数字)和javascript(我使用jQUery)检查数字范围的组合。 The combination will allow you not messing around with a pattern in low level (limit the exact digit to be inputted). 该组合将使您不会在低级模式上乱七八糟(限制要输入的确切数字)。
However, if you still want to limit the user input, not checking afterward, you still can with oninput and manually limit out the next possible options. 但是,如果您仍然想限制用户输入而不是事后检查,则仍然可以使用oninput并手动限制下一个可能的选项。
There is a jQuery Validation plugin https://jqueryvalidation.org/ that is great for this. 有一个jQuery Validation插件https://jqueryvalidation.org/对此非常有用。
Just embed it and use: 只需嵌入并使用:
$("#year_form").validate({
lang: 'cs',
rules: {
year: {
required: true,
min: 1910,
max: 2016
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.