posted 12 years ago
I have a textbox which allows max entry length of 6 characters. `[hh:mma format]`
This textbox allows the user to enter time either in 24-hour or 12-hour format. Time interval would be 15.
For ex:
- If user enters 13, it would be converted to 01:00p
- If user enters 2, it would be converted to 02:00a (i.e if no am/pm is entered, defaults to am)
- If user enters 2:10 , it would be converted to 02:15a
- If user enters 2:40p , it would be converted to 02:45p
- If user enters only : , it would be set as 00:00a
- If user enters only :a or p or a , alert msg 'Invalid time' would be displayed
For invalid entries, i.e entries other than [0-9:ap], alert message would be displayed like 'Invalid Time entered'.
I need help writing the code for it in Javascript.
Please find below, a portion of the code that I wrote. Although my code does work partially, the code seems to be way way way too long!! Is there any easier way of doing it?..say with regex?
Please help. Thnx in advance.