简体   繁体   English

验证输入到文本框中的时间

[英]Validating time entered into textbox

I have a textbox in which user can enter time (eg: 01:00) and also a drop down box for entering AM/PM fields. 我有一个文本框,用户可以在其中输入时间(例如:01:00),还有一个下拉框,用于输入AM / PM字段。 (Since the AM/PM field is used, 12-hour time format is used.) (由于使用了AM / PM字段,所以使用了12小时制。)

The text box allows a max entry of 5 chars only (eg: 01:00). 该文本框最多只能输入5个字符(例如:01:00)。

  1. How can I set the 3rd char as a default a colon : , so that the user simply has to enter only the time. 如何将第三个字符设置为默认冒号: ,以便用户仅需输入时间即可。
  2. How to check if the time entered by the user is numeric or not?. 如何检查用户输入的时间是否为数字?
  3. Autocomplete feature: for example, if the user enters 1 then it would automatically be set to 01:00 自动完成功能:例如,如果用户输入1,则它将自动设置为01:00
  4. JavaScript Validations for 12-hour format. 12小时格式的JavaScript验证。 Eg: if user enters 13:00 then it should change to 01:00 例如:如果用户输入13:00,则应更改为01:00
  5. How can I append the text box time values with the am/pm value selected in the drop down box?. 如何在文本框中添加在下拉框中选择的am / pm值的时间值?
  6. Once the values are appended, automatically populate another text box (text box 2) with the result. 附加值后,将自动使用结果填充另一个文本框(文本框2)。 Eg: 01:00 + pm should be set as 01:00p in the new text box (text box 2). 例如:在新文本框(文本框2)中,应将01:00 + pm设置为01:00p。

Any help would be appreciated. 任何帮助,将不胜感激。

  1. Better to have textbox with size and maxlength of 2 named "txtHours", colon as label (span tag) and another textbox named "txtMinutes". 最好使用大小和最大长度为2的文本框,名称为“ txtHours”,冒号作为标签(span标签),而另一个文本框的名称为“ txtMinutes”。 Much easier to manage. 更容易管理。
  2. Instead of checking, have onkeypress code that returns false when the key pressed is not a number thus allowing only digits. 无需检查,而具有onkeypress代码,当所按的键不是数字时,该键返回false,从而仅允许数字。
  3. Best be done in the onblur event, if the value is less than 10 add zero in the beginning. 最好在onblur事件中完成,如果该值小于10,请在开头添加零。
  4. Pretty simple script, though not trivial - use % operator. 非常简单的脚本,尽管不简单-使用%运算符。
  5. Not sure what you mean? 不明白你的意思? Append where? 附加在哪里?
  6. document.getElementById("textbox2").value = strAppendedText;

That's the general idea, let me know if you need help implementing something best give it a try yourself first. 这是一般的想法,如果您需要帮助以最好的方式实施某些东西,请先尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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