用于验证字符串中日期的正则表达式 [英] Regular expression to validate date within a string
问题描述
大家好,
如何验证字符串中的日期.我需要服务器端验证.我想使用正则表达式来验证字符串或语句中的日期...请为该问题建议服务器端代码.
例如:"DOB:02-02-1987"或"DOB:1987年5月14日"
谢谢
Hi All,
How to validate the date within a string..?? I want the server-side validation.I want the regular expression to validate date within a string or statement...please suggest server side code for this question.
For ex:"DOB:02-02-1987" or "DOB:14th may 1987"
Thanks
推荐答案
请参阅我的问题.我希望您可以决定不使用"14th"之类的非标准内容.
您实际上并不需要使用正则表达式.将字符串解析为日期或时间结构将是一种更可靠的方法.成功的话是有效的;如果解析失败,则不是.很简单.
在客户端,它将是这样的JavaScript:
Please see my question. I hope you can decide to work without non-standard things like "14th".
You don''t really need to use Regular Expressions. It will be way more reliable method to parse string as a date or time structure. In case of success, it''s valid; if parsing fails, it is not. Very simple.
On client side, it will be JavaScript like this:
function IsValidDate(dateString) {
return !isNaN(Date.parse(dateString));
}
在服务器端,您需要使用System.DateTime
方法Parse
或ParseExact
做同样的事情.与JavaScript不同,您需要以与所需区域性信息(IFormatProvider
)和/或格式说明符相对应的准确格式提供输入字符串.但是对于Parse
方法,有一些变体:仅日期,仅时间,两者,并且区域性应与当前线程区域性相对应.
此外,您需要在try-catch块中调用此类方法,因为它可以捕获异常.例如:
On server side, you need to do the same thing using System.DateTime
methods Parse
or ParseExact
. Unlike JavaScript, you will need to supply input string in exact format corresponding to required culture information (IFormatProvider
) and/or format specifiers; but for Parse
method, there are variants: date only, time only, both, and the culture should correspond current thread culture.
Besides, you need to call such method in the try-catch block as it can catch exception. For example:
static bool IsValidDate(string value) {
try {
System.DateTime.Parse(value);
return true;
} catch(System.FormatException) {
return false;
} //exception
} //IsValidDate
请参阅:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ].
有关时间字符串格式,请参见:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].
Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].
For time string formats, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].
客户端日期日期类型的日期验证:"02-02-1987"
在客户端使用文本字段日期,双精度类型(全球化中的小数位或不同的十进制字符等)和货币验证时,我们经常使用正则表达式验证器并尝试编写复杂的正则表达式.验证这些数据类型的最简单方法是使用比较验证器.我认为这是用于验证数据类型的最强大,最强大的控件.您要做的就是设置
运算符="DataTypeCheck"
还有
类型=<<<您在这里需要的类型>>>"
它支持以下类型:
日期
货币
Double
整数
字符串
看下面的例子:
这将根据应用程序的当前区域性来验证日期.
Client Side Date Validation for Date Format of type: "02-02-1987"
When using text field date, Double type (decimal places or different decimal characters in globalization etc) and currency validation on client-side we often use regular expression validator and try to write complex regular expressions. The easiest way to validate these data types is to use compare validator. I think it’s the most powerful and awesome control for validating data types. All you have to do is to set
Operator="DataTypeCheck"
And
Type="<<your required type here >>>"
It supports following types:
Date
Currency
Double
Integer
String
Look at the following examples:
This validates the date according to current culture of the application.
<asp:textbox id=""txtDateStart"" cssclass=""input"" runat=""server"" width=""200″" xmlns:asp="#unknown">
<asp:comparevalidator id=""cmprValidatorDateStart"" controltovalidate=""txtDateStart"" xmlns:asp="#unknown">
Type="Date" Display="Dynamic" Operator="DataTypeCheck"
ErrorMessage="*Not a valid date." runat="server"<>/asp:CompareValidator>
在后面的代码中使用DateTime.TryParseExact
函数.
In code behind use the DateTime.TryParseExact
function.
这篇关于用于验证字符串中日期的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!