我需要正则表达式来验证日期格式 ddmmyy 。
答案 0 :(得分:0)
K,我将继续并假设您还需要验证它是否为有效日期。所以...这样做:
<asp:TextBox id="txtDate" runat="server" MaxLength="6" />
<asp:CustomValidator runat="server" ControlToValidate="txtDate" OnServerValidate="validate_Date" ErrorMessage="Invalid (ddmmyy)" />
代码背后:
protected void validate_Date(object sender, ServerValidateEventArgs args)
{
System.Text.RegularExpressions.Regex rDate = new System.Text.RegularExpressions.Regex(@"^(?<day>[0-9]{2})(?<mon>[0-9]{2})(?<year>[0-9]{2})");
System.Text.RegularExpressions.Match m = rDate.Match(args.Value);
if (m.Success)
{
//Now try parse as a date
DateTime dtCheck = DateTime.MinValue;
args.IsValid = DateTime.TryParse(String.Format("20{0}-{1}-{2}", m.Groups["year"].Value, m.Groups["mon"].Value, m.Groups["year"].Value), out dtCheck);
}
}