• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Time entry validations

 
Greenhorn
Posts: 5
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't indent your code?

Use regular expression(s).
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use capture groups so you know what you have from the start



 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just code this for fun and it was a fun little problem.

Stuff that the requirements do not say is how you round to the 15. 7 would be 0 or 15?
What happens if someone enters >= 24

The logic that makes this long is the rounding to neareat 15. I been trying to figure out a cleaner way.

The problem with your code is you do the same checks over and over again. Normalize the data up front and than do the processing

Check to see if you have an invalid [no match], :, :a, :p entry.
Set the hours/minutes/AM/PM right away. If you do not have a value set it to a default.
Check hours to see if it is > 12 and adjust the amPM
round minutes

Combine together checking if you need the padding for the single digit hours/minutes.

Eric
 
Sony Mark
Greenhorn
Posts: 5
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help , Eric. I will try to simplify my code, based on your suggestions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic