[英]Date Validation - Only enter dates within current month
我已經分配了驗證日期字段的任務,該日期字段在創建發票時會被填充。 它是帶有三個按鈕對象的文本框,允許用戶從日歷中選擇日期,輸入今天的日期或刪除日期條目。
我的任務是確保用戶不能輸入不在當前月份內的日期(雙否定... tric)。 我的任務是確保用戶只能輸入當月內的日期。 (更好?)
我不知道該怎么做。 我應該使用asp控件還是在后端使用它?
我正在使用VB.NET。
使用ASP.NET Validator
控件,如下所示:
標記:
<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
正在調用該函數,然后將其作為要比較的值傳遞給驗證器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.