C# 用CustomValidator验证控件验证输入的是不是日期。DateValitor这个方法怎么写才能输出ErrorMessage 10
<body><formid="form1"runat="server"><divclass="myStyle1">请输入出生日期:<asp:TextBoxID="Text...
<body>
<form id="form1" runat="server">
<div class="myStyle1">
请输入出生日期:<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" CausesValidation="true"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="DateValitor" ControlToValidate=
"TextBox1" ErrorMessage="出生日期格式不正确,正确的格式为YEAR-M-D"></asp:CustomValidator>
</div>
</form>
</body>
我写的方法,但是结果不是想要的
protected void DateValitor(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
DateTime date = new DateTime();
try
{
date = DateTime.Parse(args.Value.ToString());
}
catch(Exception e)
{
Response.Write("出生日期格式不正确,正确的格式为YEAR-M-D");
}
finally
{
if (date != null)
{
args.IsValid = true;
}
}
} 展开
<form id="form1" runat="server">
<div class="myStyle1">
请输入出生日期:<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" CausesValidation="true"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="DateValitor" ControlToValidate=
"TextBox1" ErrorMessage="出生日期格式不正确,正确的格式为YEAR-M-D"></asp:CustomValidator>
</div>
</form>
</body>
我写的方法,但是结果不是想要的
protected void DateValitor(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
DateTime date = new DateTime();
try
{
date = DateTime.Parse(args.Value.ToString());
}
catch(Exception e)
{
Response.Write("出生日期格式不正确,正确的格式为YEAR-M-D");
}
finally
{
if (date != null)
{
args.IsValid = true;
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询