如何检查或验证文本框输入的日期是否为 DD/MM/YYYY
格式?
最佳答案
标记:
<asp:Textbox runat="server" ID="TextBox1" />
<asp:CustomValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Date was in incorrect format" OnServerValidate="CustomValidator1_ServerValidate" />
代码隐藏:
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
DateTime d;
e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
}
如果你想允许几种格式并且只允许它们,请使用下一个:
DateTime.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);
关于c# - 如何检查或验证文本框输入的日期是 DD/MM/YYYY 格式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3005425/