vb.net 带有 textmode=date 的 Asp 文本框无法验证大于当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19920204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asp textbox with textmode=date cannot validate greater than current date
提问by user2943426
I have a asp textbox with the type=DateTimeLocal. I am trying to validate it it to make sure that it's datetime value is greater than the current datetime. I have tried compare validators loading values both in aspx and vb(server) side. I have tried range validators and custom validators. My Custom validators never trigger. Range and Compare always returns that is is invalid.
我有一个 type=DateTimeLocal 的 asp 文本框。我试图验证它以确保它的日期时间值大于当前日期时间。我尝试比较验证器在 aspx 和 vb(server) 端加载的值。我尝试过范围验证器和自定义验证器。我的自定义验证器永远不会触发。范围和比较总是返回无效的。
How can I validate that the text entered in the textbox with the type=DateTimeLocal provided is greater than current date time?
如何验证在提供的 type=DateTimeLocal 的文本框中输入的文本大于当前日期时间?
ASPX Page
ASPX 页面
<asp:TextBox CssClass="form-control" ID="txtStartDate" TextMode="DateTimeLocal" runat="server" type="date"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvStartDate" runat="server" ErrorMessage="Start Date is required" Text="*"
ControlToValidate="txtStartDate" ValidationGroup="AddEvent"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rgvStartDate" runat="server" ErrorMessage="Start Date must be a date after the current Date"
text="*" ValidationGroup="AddEvent" ControlToValidate="txtStartDate" Type="Date"></asp:RangeValidator>
===================================
====================================
Back End
后端
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
rgvStartDate.MinimumValue = DateTime.Now.Date.AddDays(1.0).ToString("d")
rgvStartDate.MaximumValue = DateTime.Now.Date.AddDays(100.0).ToString("d")
End Sub
===================================== Solution
====================================== 解决方法
<div class="form-group">
<label class="col-lg-4 control-label" for="txtStartDate">Start Date:</label>
<div class="col-lg-8">
<asp:TextBox CssClass="form-control" ID="txtStartDate" TextMode="Date" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvStartDate" runat="server" ErrorMessage="Start Date is required" Text="*"
ControlToValidate="txtStartDate" ValidationGroup="AddEvent"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rgvStartDate" runat="server" ErrorMessage="Start Date must be a date after the current Date"
text="*" ValidationGroup="AddEvent" ControlToValidate="txtStartDate" Type="Date"></asp:RangeValidator>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="txtStartDateTime">Start Time:</label>
<div class="col-lg-8">
<asp:TextBox CssClass="form-control" ID="txtStartDateTime" TextMode="Time" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvStartDateTime" runat="server" ErrorMessage="Start Time is required" Text="*"
ControlToValidate="txtStartDateTime" ValidationGroup="AddEvent"></asp:RequiredFieldValidator>
</div>
</div>
Originally I had the time and date both inside the same textbox, however this caused my validations not to work. The solution was to make a separate textbox for time, and then let date be by itself. Then when you go to add, you can put the date and time together.
最初我将时间和日期都放在同一个文本框中,但这导致我的验证不起作用。解决方案是为时间制作一个单独的文本框,然后让日期单独存在。然后当你去添加时,你可以把日期和时间放在一起。
<asp:TextBox CssClass="form-control" ID="txtStartDate" TextMode="DateTimeLocal" runat="server" type="date"></asp:TextBox>
<asp:TextBox CssClass="form-control" ID="txtStartDate" TextMode="DateTimeLocal" runat="server" type="date"></asp:TextBox>
回答by Md. Ashaduzzaman
You need to change the minimum range of the validator programmatically on Page_Load. You also have to set the minimum and maximum values of the validator with some dummy formats (Min - 1/1/1999 , Max - 1/1/2045).
您需要在 Page_Load 上以编程方式更改验证器的最小范围。您还必须使用一些虚拟格式(Min - 1/1/1999,Max - 1/1/2045)设置验证器的最小值和最大值。
This will set the minimum value of the validator with the date you want.One day plus with the current date.
这将使用您想要的日期设置验证器的最小值。一天加上当前日期。
DateRangeValidator.MinimumValue = DateTime.Now.Date.AddDays(1.0).ToString("d");
This should work.
这应该有效。

