0

I need regular expression for validating datetime as "MM/DD/YYYY HH:MM" where time is in 24hr format.

Can anybody help me solve this.

Thanks for sharing your wisdom.

IrfanRaza
  • 3,030
  • 17
  • 64
  • 89
  • 2
    Why not [DateTime.Parse](http://msdn.microsoft.com/en-us/library/kc8s65zs.aspx) instead of regex? – Vikas May 16 '12 at 12:08
  • I want this expression in RegularExpression validator control of asp.net for validating datetime from textbox. – IrfanRaza May 16 '12 at 12:09
  • @IrfanRaza, i've no experience with asp.net, but i'll be surprised if you can't write a validator that uses DateTime.Parse and return true/false accordingly. – Vikas May 16 '12 at 12:12
  • 1
    Using regular expression for that? What about the 31st of February? And all exceptions like that? Use DateTime.Parse! – David Brabant May 16 '12 at 12:59
  • See http://stackoverflow.com/questions/939802/date-validation-with-asp-net-validator. The second answer has a validation function that compares a date to see if it's within a specified range. You could easily modify that to use `DateTime.TryParseExact` for your validation. – Jim Mischel May 16 '12 at 13:49

1 Answers1

0

You can use a RangeValidator with a given rage of dates or a CustomValidator and then use DateTime.Parse from that

msam
  • 4,259
  • 3
  • 19
  • 32