vb.net 验证文本框中的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22460847/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:07:56  来源:igfitidea点击:

Validating a date in a textbox

vb.netwinformsdatetime

提问by ghost_king

I am trying to valid user input in a textbox which will only takes dates or an empty value (hence the textbox vs a date time picker). Here are the conditions:

我正在尝试在文本框中验证用户输入,该文本框中只需要日期或空值(因此文本框与日期时间选择器)。以下是条件:

  • Only a date value ("dd-mm-yyyy" or "dd-mm-yy)
  • Must contain only slashes or numbers
  • The date has to be on the day it is being typed in
  • 只有一个日期值(“dd-mm-yyyy”或“dd-mm-yy”)
  • 必须只包含斜杠或数字
  • 日期必须是输入日期

This is what I have so far:

这是我到目前为止:

Private Sub tbApp1_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbApp1.TextChanged
        If Not Me.tbApp1.Text = String.Empty Then
            If Not DateTime.TryParseExact(tbApp1.Text.Trim, formats, New Globalization.CultureInfo("en-US"), Globalization.DateTimeStyles.None, dtTemp) Then
                If Not tbApp1.Text.Trim = DateTime.Today.Date Then
                    ErrorProvider1.SetError(tbApp1, "This is not a valid date; Enter in this format ('M/d/yyyy' or 'M/d/yy')")
                End If
            Else
                ErrorProvider1.Clear()
            End If
        ElseIf Me.tbApp1.Text.Trim = "" Then
            ErrorProvider1.Clear()
        End If
    End Sub

Masked Textbox use

蒙版文本框使用

'Private Sub mtbApp1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles mtbApp1.TypeValidationCompleted
        If Not Me.mtbApp1.Text = String.Empty Then
            If (Not e.IsValidInput) Then
                ErrorProvider1.SetError(mtbApp1, "The data you supplied must be a valid date in the format mm/dd/yyyy.")
            Else
                ' Now that the type has passed basic type validation, enforce more specific type rules. 
                Dim UserDate As DateTime = CDate(e.ReturnValue)
                If (UserDate = DateTime.Now) Then
                    ErrorProvider1.SetError(mtbApp1, "The data you supplied must be today's date")
                    e.Cancel = True
                End If
            End If
            ErrorProvider1.Clear()
        End If
    End Sub'

I noticed for a date like 03/18/2014 when it loaded back into the masked textbox it converts to 31/82/014. How could i fix this? The query pulls back the field as

我注意到像 2014 年 3 月 18 日这样的日期,当它加载回掩码文本框时,它会转换为 31/82/014。我怎么能解决这个问题?查询将字段拉回为

CONVERT(VARCHAR(10),Date,101) AS Date

I set it in vb as :

我在 vb 中设置为:

 Dim Approval1 As Date = Nothing

and then

进而

 If Not IsDBNull(((WorklistsDS.Tables(0).Rows(0).Item("Approval1")))) Then
                Approval1 = ((WorklistsDS.Tables(0).Rows(0).Item("Approval1")))
            End If

and then loaded into the masked textbox as:

然后加载到掩码文本框中:

If Approval1 <> Nothing Then
                Me.mtbApp1.Text = Approval1
            End If

回答by Mych

You could also simplify your validation with

您还可以使用以下方法简化验证

If IsDate(tbApp1.Text) Then 
    'valid date now just check if date falled within permitted range
    Dim CompDate As Date = CDate(tbApp1.Text)
    Dim MidDate As Date = *enter your min date here*
    Dim MaxDate As Date = *enter your max date here*
    If CompDate >= MinDate AndAlso CompDate <= MaxDate Then
        'Date is within permitted range
        ......
    Else
        'Date outside range
        'Display error
    End If
Else
   'text in tbApp1 is not a date
   'Display Error
End If