比较两个日期的问题
我使用两个CalendarExtender
来组合两个日期:开始日期和结束日期,并使用比较验证器
来验证结束日期晚于开始日期。
当结束日期中的日期小于开始日期中的日期时,即使整个结束日期大于整个开始日期,验证器也会触发问题。
如何解决此问题?
我的 aspx:
1-开始日期:
<asp:TextBox ID="txt_startDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_startDate"
ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<cc1:MaskedEditExtender ID="txt_startDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_startDate">
</cc1:MaskedEditExtender>
<cc1:CalendarExtender ID="txt_startDate_CalendarExtender" runat="server" Enabled="True"
TargetControlID="txt_startDate" Format="dd/MM/yyyy">
</cc1:CalendarExtender>
2-结束日期:
<asp:TextBox ID="txt_endDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<cc1:CalendarExtender ID="txt_endDate_CalendarExtender" runat="server" TargetControlID="txt_endDate"
Format="dd/MM/yyyy">
</cc1:CalendarExtender>
<cc1:MaskedEditExtender ID="txt_endDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_endDate">
</cc1:MaskedEditExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txt_endDate"
Display="Dynamic" ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="txt_startDate"
ControlToValidate="txt_endDate" Display="Dynamic" ErrorMessage="????? ??????? ??? ?? ???? ??? ?? ????? ???????"
Operator="GreaterThan" Type="Date" ValidationGroup="insertgroup"></asp:CompareValidator>
编辑: 两个日期的示例会产生问题:
//error message
start date:
28/01/2014
end date :
25/07/2014
while
// no error message
start date:
28/01/2014
end date :
01/07/2014
i use two CalendarExtender
to combine two dates: start date and end date and i use a compare validator
to validate that the end date is greater than the start date.
the problem that the validator fires when the day in the end date is smaller than the day in the start date even though the whole end date is greater than the whole start date ..
how to fix this problem?
my aspx:
1-start date:
<asp:TextBox ID="txt_startDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_startDate"
ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<cc1:MaskedEditExtender ID="txt_startDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_startDate">
</cc1:MaskedEditExtender>
<cc1:CalendarExtender ID="txt_startDate_CalendarExtender" runat="server" Enabled="True"
TargetControlID="txt_startDate" Format="dd/MM/yyyy">
</cc1:CalendarExtender>
2-end date:
<asp:TextBox ID="txt_endDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<cc1:CalendarExtender ID="txt_endDate_CalendarExtender" runat="server" TargetControlID="txt_endDate"
Format="dd/MM/yyyy">
</cc1:CalendarExtender>
<cc1:MaskedEditExtender ID="txt_endDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_endDate">
</cc1:MaskedEditExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txt_endDate"
Display="Dynamic" ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="txt_startDate"
ControlToValidate="txt_endDate" Display="Dynamic" ErrorMessage="????? ??????? ??? ?? ???? ??? ?? ????? ???????"
Operator="GreaterThan" Type="Date" ValidationGroup="insertgroup"></asp:CompareValidator>
EDIT:
Example for two dates make problems:
//error message
start date:
28/01/2014
end date :
25/07/2014
while
// no error message
start date:
28/01/2014
end date :
01/07/2014
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是有关为什么您会出现此行为以及为什么 Chad 的代码会对您有所帮助的更多信息。
现在,您的应用程序正在文化设置下运行,该文化设置表示日期应为 mm/dd/yyyy(您的计算机或网络服务器像美国人一样工作)。如果您查看 ASP.NET 为验证器发出的 javascript,您会发现在进行比较时,它首先检查 ControlToValidate 的数据类型是否正确。它通过使用正则表达式解析输入的值来实现此目的,并期望首先是月份值,然后是日期(由于区域性设置)。当它看到您的值为 25 时,它认为这是无效的月份,因此表示您的日期无效。然后它返回 false,甚至不检查第二个字段的日期值或尝试比较它们。
在第二个示例中,验证通过,因为它首先检查结束日期的数据类型,该数据类型可以是有效的 mm/dd/yyyy 日期。然后它检查第二个日期的数据类型,由于 28,导致它认为这不是有效日期。然后验证器返回 true,因为它假定有效日期大于无效日期。
Chad 的代码会将页面线程的区域性设置为使用 dd/MM/yyyy 格式的区域性,因此您的验证器在检查日期是否有效时将使用该格式。
以下是包含详细信息的 MSDN 页面关于文化背景及其所做的事情。
Here's some more information as to why you're getting this behavior, and why Chad's code will help you.
Right now your application is running under a culture setting that says dates should be mm/dd/yyyy (your computer or the webserver is working like an American). If you look at the javascript that ASP.NET emits for the Validators, you'll see that when doing a comparison it first checks whether the datatype of the ControlToValidate is correct. It does this by parsing the entered value with a RegEx, and expects the month value to be first and then the day (because of the culture setting). When it sees your value of 25, it doesn't think this is a valid month, and hence says that your date is not valid. It then returns false, without even checking the second field's date value or trying to compare them.
In your second example, validation passes because first, it checks the datatype of the end date, which CAN be a valid mm/dd/yyyy date. Then it checks the datatype of the second date, which because of the 28, causes it to think it's not a valid date. The validator then returns true, because it assumes that a valid date is greater than a non-valid date.
Chad's code will set the culture of your page's thread to one that uses the dd/MM/yyyy format, and therefore your validators will use that when checking whether your dates are valid.
Here's the MSDN page with more info about the culture settings and what they do.
尝试将其放在页面加载的代码后面
Try putting this in the page load in the code behind