C#正则表达式验证日期? [英] C# Regular Expression to validate a date?

查看:313
本文介绍了C#正则表达式验证日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证输入到文本框中的日期。文本框上有一个输入掩码,强制输入xx / xx / xxxx。我试图使用正则表达式验证器来强制输入正确的日期。我根本不熟练RegEx。我的同事在互联网上找到了这个,但是我不知道该怎么做。

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 validator to enforce that a correct date is entered. I am not skilled in RegEx bascially at all. My co-worker found this one on the internet but I can't really tell what it's doing.

这样看起来不错吗?似乎过于复杂...

Does this look right? Seems overly complicated...

(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)



< ?

Does anyone know of a less complex expression that essentially does what I need?

推荐答案

为什么不使用 System.DateTime 命名空间?您可以使用 DateTime.TryParse() 编辑: DateTime.TryParseExact() 可能是正确的建议)来完成验证。

Why not use one of the methods available in the System.DateTime namespace? You could use DateTime.TryParse() (edit: DateTime.TryParseExact() is probably the right suggestion) to accomplish the validation.

这篇关于C#正则表达式验证日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆