使用ASP.NET验证器进行日期验证

我正在尝试使用ASP.NET RangeValidator来验证文本框上的日期。在文本框中输入的日期格式为dd MMMM yyyy。


如何使用范围验证器来验证有效日期?如果我输入1000年1月1日作为最小值或最大值,则会出现一个错误,提示无法将值转换为日期类型,但是如果我使用其他格式,它将输入的文本视为无效。


下面是我的代码:


<asp:TextBox 

    runat="server" 

    ID="txtDatecompleted" 

/>

<cc2:CalendarExtender

    ID="datecompletedExtender" 

    runat="server"

    TargetControlID="txtDatecompleted"

    Format="dd MMMM yyyy"

/>  

<asp:RangeValidator 

    runat="server" 

    ID="RangeValidator1" 

    Type="Date" 

    ControlToValidate="txtDatecompleted" 

    MaximumValue="9999/12/28" 

    MinimumValue="1000/12/28" 

    ErrorMessage="enter valid date" 

    Display="None"

/>

<cc2:ValidatorCalloutExtender 

    ID="RangeValidator1_ValidatorCalloutExtender" 

    runat="server"

    Enabled="True"

    TargetControlID="RangeValidator1">

</cc2:ValidatorCalloutExtender>


小唯快跑啊
浏览 573回答 3
3回答

慕勒3428872

CustomValidator也可以在这里工作:<asp:CustomValidator runat="server"&nbsp; &nbsp; ID="valDateRange"&nbsp;&nbsp; &nbsp; ControlToValidate="txtDatecompleted"&nbsp; &nbsp; onservervalidate="valDateRange_ServerValidate"&nbsp;&nbsp; &nbsp; ErrorMessage="enter valid date" />后台代码:protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args){&nbsp; &nbsp; DateTime minDate = DateTime.Parse("1000/12/28");&nbsp; &nbsp; DateTime maxDate = DateTime.Parse("9999/12/28");&nbsp; &nbsp; DateTime dt;&nbsp; &nbsp; args.IsValid = (DateTime.TryParse(args.Value, out dt)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && dt <= maxDate&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && dt >= minDate);}

繁花如伊

我认为必须在当前应用程序区域中指定日期。您可能想尝试将CultureInvariantValues设置为true,看看是否可以解决您的问题。否则,您可能需要更改当前区域性(或区域性本身)的DateTimeFormat以获得所需的内容。
打开App,查看更多内容
随时随地看视频慕课网APP