1

我被分配了验证创建发票时填充的日期字段的任务。它是一个带有三个按钮对象的文本框,允许用户从日历中选择日期、输入今天的日期或删除日期条目。

我的任务是确保用户不能输入不在当月内的日期(双重否定......棘手)。我的任务是确保用户只能输入当月内的日期。(更好的?)

我不知道该怎么做。我应该使用 asp 控件还是在后端执行此操作?

我正在使用 VB.NET。

4

1 回答 1

2

使用 ASP.NETValidator控件,如下所示:

标记:

<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" />

注意:我已经包含了RequireFieldValidator以确保我们有一个值来比较日期验证。

代码隐藏(Page_Load):

If Not IsPostBack Then
    Dim firstOfTheMonthDate As DateTime = FirstDayOfMonthFromDateTime(DateTime.Now)
    Me.compareValidatorDate.ValueToCompare = firstOfTheMonthDate.ToString("d")
End If

代码隐藏(实用功能):

Public Function FirstDayOfMonthFromDateTime(dateTime As DateTime) As DateTime
    Return New DateTime(dateTime.Year, dateTime.Month, 1)
End Function

注意:我包含了一个函数来确定当月第一天的日期。正在调用该Page_Load函数,然后将其作为要比较小于的值传递给验证器。

于 2013-08-20T18:22:42.403 回答