How to validate Date entries.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • YardDancer

    #1

    How to validate Date entries.

    Hi All,

    Does anybody have a routine for validating a date or DateTime entry in a
    textbox.

    I know i can use one of the DateTime.Parse( textbox.text) methods which
    would throw a Fromat Exception.

    What i am after is something like a IsNumeric() fuction or way i can test a
    value if it is dateTime value and recieve a boolean answer i.e. true or
    false.

    Yard Dancer
    Practicing at home before I attempt to dance at the hall.


  • Ken Tucker [MVP]

    #2
    Re Dim dtnow As DateTime: How to validate Date entries.

    Hi,

    If you are using vb 2005 I would use DateTime.TryPar se otherwise
    I would use a regex.

    http://msdn2.microsoft.com/en-us/lib....tryparse.aspx

    Dim dtnow As DateTime

    If DateTime.TryPar se("1/1/2006", dtnow) Then
    Me.Text = dtnow.ToString
    End If

    Regex example

    Dim dtNow As DateTime
    Dim strDate As String = "1/1/2006"
    Dim regDate As New
    System.Text.Reg ularExpressions .Regex("^\d{1,2 }\/\d{1,2}\/\d{4}$")
    If regDate.IsMatch (strDate) Then
    dtNow = Date.Parse(strD ate)
    Me.Text = dtNow.ToString
    End If



    Ken
    ---------------------
    "YardDancer " <dancer_yard@ho tmail.comwrote in message
    news:OiLzXP9sGH A.1876@TK2MSFTN GP06.phx.gbl...
    Hi All,
    >
    Does anybody have a routine for validating a date or DateTime entry in a
    textbox.
    >
    I know i can use one of the DateTime.Parse( textbox.text) methods which
    would throw a Fromat Exception.
    >
    What i am after is something like a IsNumeric() fuction or way i can test
    a value if it is dateTime value and recieve a boolean answer i.e. true or
    false.
    >
    Yard Dancer
    Practicing at home before I attempt to dance at the hall.
    >

    Comment

    • YardDancer

      #3
      Re: Re Dim dtnow As DateTime: How to validate Date entries.

      I am Programming with VB.NET 2003

      --
      Yard Dancer
      Practicing at home before I attempt to dance at the hall.
      "Ken Tucker [MVP]" <vb2ae@bellsout h.netwrote in message
      news:ux3w9g9sGH A.4612@TK2MSFTN GP06.phx.gbl...
      Hi,
      >
      If you are using vb 2005 I would use DateTime.TryPar se
      otherwise I would use a regex.
      >
      http://msdn2.microsoft.com/en-us/lib....tryparse.aspx
      >
      Dim dtnow As DateTime
      >
      If DateTime.TryPar se("1/1/2006", dtnow) Then
      Me.Text = dtnow.ToString
      End If
      >
      Regex example
      >
      Dim dtNow As DateTime
      Dim strDate As String = "1/1/2006"
      Dim regDate As New
      System.Text.Reg ularExpressions .Regex("^\d{1,2 }\/\d{1,2}\/\d{4}$")
      If regDate.IsMatch (strDate) Then
      dtNow = Date.Parse(strD ate)
      Me.Text = dtNow.ToString
      End If
      >

      >
      Ken
      ---------------------
      "YardDancer " <dancer_yard@ho tmail.comwrote in message
      news:OiLzXP9sGH A.1876@TK2MSFTN GP06.phx.gbl...
      >Hi All,
      >>
      >Does anybody have a routine for validating a date or DateTime entry in a
      >textbox.
      >>
      >I know i can use one of the DateTime.Parse( textbox.text) methods which
      >would throw a Fromat Exception.
      >>
      >What i am after is something like a IsNumeric() fuction or way i can
      >test a value if it is dateTime value and recieve a boolean answer i.e.
      >true or false.
      >>
      >Yard Dancer
      >Practicing at home before I attempt to dance at the hall.
      >>
      >
      >

      Comment

      • Kerry Moorman

        #4
        RE: How to validate Date entries.

        YardDancer,

        What about VB's IsDate function?

        Kerry Moorman

        "YardDancer " wrote:
        Hi All,
        >
        Does anybody have a routine for validating a date or DateTime entry in a
        textbox.
        >
        I know i can use one of the DateTime.Parse( textbox.text) methods which
        would throw a Fromat Exception.
        >
        What i am after is something like a IsNumeric() fuction or way i can test a
        value if it is dateTime value and recieve a boolean answer i.e. true or
        false.
        >
        Yard Dancer
        Practicing at home before I attempt to dance at the hall.
        >
        >
        >

        Comment

        • Rad Dec [C# MVP]

          #5
          Re: How to validate Date entries.

          It's tricky to write such a validation routine because "invalid date" is
          quite relative.

          For instance "13/1/2006" is a valid in some regions but not in others.

          If you know the region you can do this:

          string UK="13/1/2006";
          DateTime UKDate = DateTime.Parse( UK,new System.Globaliz ation.CultureIn fo("en-gb"));

          You could probably put this in a function and catch any format exceptions
          and return false.

          Hi All,
          >
          Does anybody have a routine for validating a date or DateTime entry
          in a textbox.
          >
          I know i can use one of the DateTime.Parse( textbox.text) methods
          which would throw a Fromat Exception.
          >
          What i am after is something like a IsNumeric() fuction or way i can
          test a value if it is dateTime value and recieve a boolean answer i.e.
          true or false.
          >
          Yard Dancer
          Practicing at home before I attempt to dance at the hall.

          Comment

          • YardDancer

            #6
            Re: How to validate Date entries.

            Hi Kerry,

            I want to write all my routines using methods and properties that are
            consistent
            with the .Net Framework class library. I am trying to avoid using
            functions
            from the VB run-time library.

            --
            Yard Dancer
            Practicing at home before I attempt to dance at the hall.


            Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.comwrote in message
            news:BFFACFA5-4EBF-49FA-8135-5599A1AE817C@mi crosoft.com...
            YardDancer,
            >
            What about VB's IsDate function?
            >
            Kerry Moorman
            >
            "YardDancer " wrote:
            >
            >Hi All,
            >>
            >Does anybody have a routine for validating a date or DateTime entry in a
            >textbox.
            >>
            >I know i can use one of the DateTime.Parse( textbox.text) methods which
            >would throw a Fromat Exception.
            >>
            >What i am after is something like a IsNumeric() fuction or way i can
            >test a
            >value if it is dateTime value and recieve a boolean answer i.e. true or
            >false.
            >>
            >Yard Dancer
            >Practicing at home before I attempt to dance at the hall.
            >>
            >>
            >>


            Comment

            Working...