日期验证与ASP.NET验证 [英] Date validation with ASP.NET validator
问题描述
我试图使用ASP.NET RangeValidator控件
来验证在文本框的日期。在文本框输入的日期的格式为 DD MMMM YYYY
。
我如何使用范围验证器来验证一个有效的日期?如果我输入的 1000年1月1日作为最小或最大值我得到一个错误说值无法转换为类型的日期,但如果我用另一种格式,它拿起我输入的文本为无效。
下面是我的code:
< ASP:文本框
=服务器
ID =txtDatecompleted
/>
< CC2:CalendarExtender
ID =datecompletedExtender
=服务器
的TargetControlID =txtDatecompleted
格式=DD MMMM YYYY
/>
< ASP:RangeValidator控件
=服务器
ID =RangeValidator1
类型=日期
的ControlToValidate =txtDatecompleted
MaximumValue =9999/12/28
MinimumValue =1000年12月28日
的ErrorMessage =请输入有效日期
显示=无
/>
< CC2:ValidatorCalloutExtender
ID =RangeValidator1_ValidatorCalloutExtender
=服务器
启用=真
的TargetControlID =RangeValidator1>
< / CC2:ValidatorCalloutExtender>
最好的办法是
一个比较验证器添加到Web表单。设置它的ControlToValidate。其类型设置属性日期。它的运营商属性设置为DataTypeCheck例如:
< ASP:CompareValidator
ID =dateValidator=服务器
类型=日期
操作=DataTypeCheck
的ControlToValidate =txtDatecompleted
的ErrorMessage =请输入一个有效的日期。>
< / ASP:CompareValidator>
I'm trying to use an ASP.NET RangeValidator
to validate a date on a textbox. The format of the date entered on the textbox is dd MMMM yyyy
.
How can I use the range validator to validate a valid date? If I enter 1 January 1000 as the min or max value I get an error saying value cannot be converted to type date, but if I use another format it picks up my entered text as invalid.
Below is my code:
<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>
Best option would be
Add a compare validator to the web form. Set its controlToValidate. Set its Type property to Date. Set its operator property to DataTypeCheck eg:
<asp:CompareValidator
id="dateValidator" runat="server"
Type="Date"
Operator="DataTypeCheck"
ControlToValidate="txtDatecompleted"
ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>
这篇关于日期验证与ASP.NET验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!