简体   繁体   English

如何验证输入到文本输入字段的时间间隔

[英]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.

相关问题 如何验证我的输入字段以请求输入一些文本? - How do I validate my input field to request some text to be entered? 选中复选框后如何验证是否在文本字段中输入了数据 - How to validate if the data is entered in the text field when the checkkbox is checked 输入文本时如何更改输入字段的背景色? - How to change the background color of an input field when text is entered? 如何在 React 的输入字段中输入的文本旁边添加永久标签 - How to add a permanent label beside entered text in an input field in React 输入文本时调整输入文本字段的大小 - Resize input text field as text is entered 如何从另一个输入文本字段 Javascript 中输入的值将值设置为输入隐藏字段 - how to set the value to a input hidden field from value entered in another input text field Javascript 输入文本时增加输入字段的大小 - Increase size of input field as text is entered 在输入字段中输入特定文本的警报 - Alert on Specific Text Entered In Input Field 在javascript的输入数字类型字段中输入值时如何显示输入文本字段? - How to show a input text field when a value is entered in input number type field in javascript? 如何验证输入文本以确保仅输入大于最小值的浮点数或整数? - How to validate input text to ensure only floats or ints greater than a minimum value are entered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM