简体   繁体   English

日期验证-仅输入当月内的日期

[英]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.

相关问题 验证当月内的日期 - Validating dates within the current month Airbnb 反应日期范围选择器仅显示当前月份日历未选择日期月份日历 - Airbnb react date range picker only showing current month calendar not selected dates month calender 在日期选择器中进行验证,仅显示当前日期起的最近5个月的日期 - validation in date picker, only show last 5 month date from current date 仅按年,月和日期比较日期? - Comparing dates only by year, month and date? 过滤从当月第一天到当前日期的日期 - Filter dates from the first day of the month to the current date 验证日期-用户只能选择下个月的日期 - Validate date - User should only select dates from next month 仅当日期变量在当前月份中时才在表中显示行 - Show row in table only if date variable is in current month 相对于在另一个文本框中选择月份,如何仅在日期选择器中显示所选月份的日期? - How to display selected month dates only in date picker with respect to selecting month in another text box? 如何使用 javascript 在包含元素的表格中显示当前月份的日期? - How do I display dates of the current month within a table with <td> elements using javascript? 我只需要在开始日期选择的日期选择器中显示未来一个月的日期 - I need to show only future one month dates in the date picker from the start date selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM