C# Regular Expression to validate a date?

后端 未结 10 1657
后悔当初
后悔当初 2020-12-30 04:45

I am trying to validate a date entered into a text box. There is an input mask on the textbox which forces input of xx/xx/xxxx. I am trying to use a regular expression valid

相关标签:
10条回答
  • 2020-12-30 05:39

    We can use a CustomValidator and use the override the ServerValidate method to check the TryParse!

    0 讨论(0)
  • 2020-12-30 05:40

    This would be correct regular expression to use for date format dd/mm/yyyy

    ^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
    
    0 讨论(0)
  • 2020-12-30 05:40

    Kettenbach had a problem. His co-worker suggested using regexs. Kettenbach then had two problems.

    As others have said, use DateTime.TryParse or DateTime.TryParseExact on a custom validator and save yourself the nightmare that is regex :)

    0 讨论(0)
  • 2020-12-30 05:40

    This isn't really an answer, but couldn't you use DateTime.Parse or DateTime.TryParse to check that the date is correct?

    Either that or use a DateTime control to make sure it's impossible to enter data that isn't a DateTime. There's lots of JavaScript out there on this subject.

    0 讨论(0)
提交回复
热议问题