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

    Join Date
    Oct 2011
    Posts
    64
    Rep Power
    5

    MVC3 RegEx question (c#)


    Hello all,

    I'm attempting to validate the date and time, separately. I think I have it right, but I just want to check my work.

    for the time I have:
    PHP Code:
    ^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$ 
    for the date I have:
    PHP Code:
    ^(?:2?[0-9][0-9][0-9]):[0-1][0-2]:(0?[1-9]|1[0-9]|2[0-9]|3[0-1])$ 
    Could anyone help verify this?

    Thanks!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    Those can be simplified.
    Code:
    time ^([01]?\d|2[0-3]):[0-5]\d:[0-5]\d$
    date ^[12]\d\d\d:(0\d|1[012]):([012]\d|3[01])$
    (\d is equivalent to [0-9])

    The time one is perfect but the date one requires more validation.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    64
    Rep Power
    5
    Originally Posted by requinix
    Those can be simplified.
    Code:
    time ^([01]?\d|2[0-3]):[0-5]\d:[0-5]\d$
    date ^[12]\d\d\d:(0\d|1[012]):([012]\d|3[01])$
    (\d is equivalent to [0-9])

    The time one is perfect but the date one requires more validation.
    PHP Code:
    ^[12]\d\d\d:(0\d|1[012]):([012]\d|3[01])$ 
    Will this be military format, though? If not, how would I know the difference in AM or PM? I've always just used military time to calculate. also, I'm trying to get the format in YYYY-MM-DD. I'm used to MySQL style databases, but I'm switching to the .Net world and working with SQL Server 2008, now.

    *** This is an MVC3 app, also. Just throwing that out there.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    That's the date regex.

    As YYYY-MM-DD it would look like the same but with hyphens instead of colons.

IMN logo majestic logo threadwatch logo seochat tools logo