[英]Date Validation - Only enter dates within current month
I've been assigned the task of validating a date field that gets populated when creating an invoice. 我已经分配了验证日期字段的任务,该日期字段在创建发票时会被填充。 It is a text box with three button objects that allow a user to select a date from a calendar, enter today's date, or remove the date entry.
它是带有三个按钮对象的文本框,允许用户从日历中选择日期,输入今天的日期或删除日期条目。
My task is to make sure that users cannot enter a date that is NOT within the current month (double negative...tricky). 我的任务是确保用户不能输入不在当前月份内的日期(双否定... tric)。 My task is to make sure that users can only enter dates within the current month.
我的任务是确保用户只能输入当月内的日期。 (better?)
(更好?)
I have no idea how to do this. 我不知道该怎么做。 Should I use the asp controls or do this on the back end?
我应该使用asp控件还是在后端使用它?
I'm using VB.NET. 我正在使用VB.NET。
Use the ASP.NET Validator
controls, like this: 使用ASP.NET
Validator
控件,如下所示:
Markup: 标记:
<asp:TextBox id="YourTextBox" runat="server" />
<asp:RequiredFieldValidator ControlToValidate="YourTextBox"
Text="The date field is required!" runat="server" />
<asp:CompareValidator ID="compareValidatorDate" ControlToValidate="YourTextBox"
Type="Date" Operator="LessThan" ErrorMessage="Date must be from this month!"
Display="Dynamic" runat="server" />
Note: I have included the RequireFieldValidator
to ensure that we have a value to compare against for the date validation. 注意:我包括了
RequireFieldValidator
以确保我们有一个值可以与日期验证进行比较。
Code-behind ( Page_Load
): 后台代码(
Page_Load
):
If Not IsPostBack Then
Dim firstOfTheMonthDate As DateTime = FirstDayOfMonthFromDateTime(DateTime.Now)
Me.compareValidatorDate.ValueToCompare = firstOfTheMonthDate.ToString("d")
End If
Code-behind (utility function): 后台代码(实用功能):
Public Function FirstDayOfMonthFromDateTime(dateTime As DateTime) As DateTime
Return New DateTime(dateTime.Year, dateTime.Month, 1)
End Function
Note: I included a function to determine the date for the first day of the current month. 注意:我提供了一个确定当前月份第一天日期的功能。 The
Page_Load
is calling that function and then passing that to the validator as the value to compare for less than. Page_Load
正在调用该函数,然后将其作为要比较的值传递给验证器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.