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. (Since the AM/PM field is used, 12-hour time format is used.)
The text box allows a max entry of 5 chars only (eg: 01:00).
- How can I set the 3rd char as a default a colon
:
, so that the user simply has to enter only the time. - How to check if the time entered by the user is numeric or not?.
- Autocomplete feature: for example, if the user enters 1 then it would automatically be set to 01:00
- JavaScript Validations for 12-hour format. Eg: if user enters 13:00 then it should change to 01:00
- How can开发者_如何学Go I append the text box time values with the am/pm value selected in the drop down box?.
- Once the values are appended, automatically populate another text box (text box 2) with the result. Eg: 01:00 + pm should be set as 01:00p in the new text box (text box 2).
Any help would be appreciated.
- Better to have textbox with size and maxlength of 2 named "txtHours", colon as label (span tag) and another textbox named "txtMinutes". Much easier to manage.
- Instead of checking, have
onkeypress
code that returns false when the key pressed is not a number thus allowing only digits. - Best be done in the
onblur
event, if the value is less than 10 add zero in the beginning. - Pretty simple script, though not trivial - use
%
operator. - Not sure what you mean? Append where?
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.
精彩评论