#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    124
    Rep Power
    17

    Time Regex Problem


    I'm using this to validate a time value.

    /^(([0-1][0-9]|2[0-3])[+][0-5][0-9])$/

    So, 23+59 is good, but 28+66 is not.

    I'd like the first digit of the hour block to be optional such that something like 3+35 validates.

    The way I have it, 3+35 does not validate, but 03+45 does.

    I'm unsure how to make that first digit optional.

    Ideas?
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,053
    Rep Power
    9616
    You already have the hour broken into 00-19 and 20-23. Break it further into 00-09, 10-19, and 20-24, then make the 0 in the first set optional.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    124
    Rep Power
    17
    This seems to work, though it might not be the correct way. I tested it with a bunch of good and bad inputs...

    /^(([0-1]?[0-9]|2[0-3])[+][0-5][0-9])$/'
  6. #4
  7. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,053
    Rep Power
    9616
    Yeah, I suppose that's easier than what I said

    Compare what it allowed before and after:
    Before: [0-1] * [0-9] = 0[0-9] + 1[0-9] = 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
    After: ([0-1] + empty) * [0-9] = 0[0-9] + 1[0-9] + [0-9] = 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9

    Looks good to me.

IMN logo majestic logo threadwatch logo seochat tools logo