C#正则表达式来验证日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1359188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Regular Expression to validate a date?
提问by Hcabnettek
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.
我正在尝试验证输入到文本框中的日期。文本框上有一个输入掩码,强制输入 xx/xx/xxxx。我正在尝试使用正则表达式验证器来强制输入正确的日期。我根本不擅长正则表达式。我的同事在互联网上找到了这个,但我真的不知道它在做什么。
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?
有谁知道一个不太复杂的表达式,它基本上可以满足我的需要?
回答by Donut
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.
为什么不使用System.DateTime
命名空间中可用的方法之一?您可以使用DateTime.TryParse()
(编辑:DateTime.TryParseExact()
可能是正确的建议)来完成验证。
回答by dtb
You can use DateTime.TryParseExact
:
您可以使用DateTime.TryParseExact
:
DateTime dt;
bool isValid = DateTime.TryParseExact(
"08/30/2009",
"MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt);
回答by Blue Toque
This isn't really an answer, but couldn't you use DateTime.Parse
or DateTime.TryParse
to check that the date is correct?
这不是真正的答案,但您不能使用DateTime.Parse
或DateTime.TryParse
检查日期是否正确?
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.
或者使用DateTime
控件来确保不可能输入不是 DateTime 的数据。有很多关于这个主题的 JavaScript。
回答by Sk93
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 :)
正如其他人所说,在自定义验证器上使用DateTime.TryParse
或DateTime.TryParseExact
并避免正则表达式的噩梦:)
回答by wwd
Last answer is actually the correct way to do. Use DateTime.TryParse
.
最后一个答案实际上是正确的做法。使用DateTime.TryParse
.
Example:
例子:
DateTime dt;
if(DateTime.TryParse(Textbox1.Text,out dt))
{
Label1.Text = "Invalid date format";
}
回答by vishal
The above regular expression is correct for dd/mm/yyyy format. the expression is
上述正则表达式对于 dd/mm/yyyy 格式是正确的。表达式是
(^((((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}))))$)
回答by Radha
This would be correct regular expression to use for date format dd/mm/yyyy
这将是用于日期格式 dd/mm/yyyy 的正确正则表达式
^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
回答by Yogi
As an alternative, you can use CompareValidator instead of RegularExpressionValidator. It goes like this:
作为替代方案,您可以使用 CompareValidator 而不是 RegularExpressionValidator。它是这样的:
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtIssueDate" ErrorMessage="Invalid Date Format" Type="Date" Operator="DataTypeCheck" Display="Dynamic" Text="*" ForeColor="Red" ValidationGroup="valGroup1"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtIssueDate" ErrorMessage="Invalid Date Format" Type="Date" Operator="DataTypeCheck" Display="Dynamic" Text="*" ForeColor="Red" ValidationGroup="valGroup1"></asp:CompareValidator>
回答by Muhammedh
We can use a CustomValidator and use the override the ServerValidate method to check the TryParse!
我们可以使用 CustomValidator 并使用覆盖 ServerValidate 方法来检查 TryParse!
回答by Mavromatos Alexandros
([0][1-9]|[1][0-9|][2][0-9]|[3][0-1])\/([0][1-9]|[1][0-2])\/[1-2][0-9][0-9][0-9]
for dd/mm/yyyy (year can be from 1000 to 2999)
对于 dd/mm/yyyy(年份可以是 1000 到 2999)
or
或者
(([0][1-9]|[2][0-9]|[3][0-1]|[1-9]|[1][0-9])/([0][1-9]|[1][0-2]|[1-9])/([1-2][0-9][0-9][0-9]|[0-9][0-9]))
(([0][1-9]|[2][0-9]|[3][0-1]|[1-9]|[1][0-9])/([0][ 1-9]|[1][0-2]|[1-9])/([1-2][0-9][0-9][0-9]|[0-9][0] -9]))
which includes d/m/yy (e.g. 1/12/82)
其中包括 d/m/yy(例如 1/12/82)