Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2003
    Posts
    241
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with a Regular Expression - time-check

    I've started looking into regExp's but it's a bit messy in my head atm, I could sure use some help!

    I want to validate the value of a textbox, with a maxlength of 5.
    The value in the textbox should be a time in this format: 14:33,
    meaning 9:45 would be incorrect but 09:45 will be correct etc.

    So... the first 2 values of the textbox has to be numeric, >= 00 and <= 24, the 3rd character has to be a :[colon] and the next 2 following characters has to be numeric, >= 00 and <= 59.

    Sure I could do this with substring's, but I'm [slowly] learning regExp's and I'm thinking it'd more efficient, but I don't really know where to start

    thanks in advance,

  • #2
    New to the CF scene
    Join Date
    Jun 2003
    Location
    Minnesota
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For a 5-byte 24-hour time regular expression, how about:

    ([01][0-9]|2[0-3]):[0-5][0-9]

    Also, here's an online regular expression tester:

    http://www.regular-expressions.info/...ptexample.html

  • #3
    Regular Coder
    Join Date
    Mar 2003
    Posts
    241
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, thats it.

    Thanks a lot!

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Don't forget the ^ and the $

    var pattern = /^([01][0-9]|2[0-3]):[0-5][0-9]$/;

    That will restrict it to only those five characters -- without them it would be any 5 in the string.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •