Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Validating MaskedTextBox defined as a Date?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] [2005] Validating MaskedTextBox defined as a Date?

    I have a maskedtextbox control on a form. It's defined as a date field using the " / / " mask. My code has a loop that checks for several different kinds of masks. My problem is when it encounters a "Date", it doesn't pass the edit because my first check is the "Length" of the field. I always check for > 0, well in this case it is but it looks like " / / ". So it passes that check. Then I check for "IsDate" and it fails there. This scenario is possible when it encounters a date field that hasn't yet been populated. How can I properly check this maskedTextBox?

    Thanks,
    Blake

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2005] Validating MaskedTextBox defined as a Date?


    So are you saying that the " / / " fails when in actuality it should pass? I would think that the " / / " failing is what you would want?

    Me thinks I'm not understanding.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: [2005] Validating MaskedTextBox defined as a Date?

    For this masked edit box, set TextMaskFormat property to "ExcludePromptAndLiterals". Doing that will cause the Length function to only count the characters entered by the user. It will not count the slashes.
    "It's cold gin time again ..."

    Check out my website here.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] Validating MaskedTextBox defined as a Date?

    Bruce,

    Your suggestion makes sense, however, I still seem to be doing alot of validation for this field. Here is what my code looks like:

    Code:
                
    For x As Integer = 0 To intElementsChosen - 1
          If arrRQElements(x).controlType <> "DATE" Then
          Else
              If txtBox.TextLength > 0 Then
                  If IsNumeric(txtBox.Text) Then
                      If IsDate(txtBox.Text) Then
                      Else
                          MessageBox.Show("Invalid Date Format!", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                          txtBox.Text = ""
                          txtBox.Focus()
                      End If
                  End If
              End If
          End If
     
          If txtBox.Name = arrRQElements(x).labelName Then
             arrRQElements(x).fieldData = txtBox.Text
             txtDetails.Enabled = True
             Exit For
          End If
    Next
    Doesn't this seem like overkill. Plus, when I type in a date value, ex: 010108, I tried converting it to a date and it didn't like it.
    Code:
          txtBox.Text = CDate(txtBox.Text)
    What would be the best way to edit this?

    Thanks,
    Blake

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: [2005] Validating MaskedTextBox defined as a Date?

    Blake, there is any number of ways to do this, but I'm thinking the easiest way is to set the TextMaskFormat property back to "IncludePromptAndLiterals" and rather than to see if the length is zero, test the control's text for a value of "__/__/____" (that would mean no text has been entered). When populated, this will make the IsDate test easier, because the IsDate function will only give you the expected result if the value being tested "looks like" a date (i.e. with slashes, dashes, or even a string like "June 17, 2008").
    I modified your code below. Note: You need not do a numeric test here.
    Code:
    For x As Integer = 0 To intElementsChosen - 1
          If arrRQElements(x).controlType <> "DATE" Then
          Else
              If txtBox.Text <> "__/__/____" Then
                  'If IsNumeric(txtBox.Text) Then
                      If IsDate(txtBox.Text) Then
                      Else
                          MessageBox.Show("Invalid Date Format!", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                          txtBox.Text = ""  ' you may want to skip clearing this (i.e. let the user see what they did wrong)
                          txtBox.Focus()
                      End If
                  'End If
              End If
          End If
     
          If txtBox.Name = arrRQElements(x).labelName Then
             arrRQElements(x).fieldData = txtBox.Text
             txtDetails.Enabled = True
             Exit For
          End If
    Next
    Also - you should only do a CDate on a string variable that "looks like" a date, as described above (with the punctuation etc.). Doing a CDate on a string of numbers will not give you the expected result or will fail.

    In some situations (not this error-checking scenario), you may find yourself writing statements like:
    If IsDate(s) Then
    MyDateVar = CDate(s)
    End If

    HTH
    Bruce
    "It's cold gin time again ..."

    Check out my website here.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Validating MaskedTextBox defined as a Date?

    There's only one validation you should be doing:
    vb.net Code:
    1. Dim d As Date
    2.  
    3. If Date.TryParse(myMaskedTextBox.Text, d) Then
    4.     'd contains the value.
    5. Else
    6.     'Invalid input.
    7. End If
    That said, why don't you just use a DateTimePicker?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] Validating MaskedTextBox defined as a Date?

    JMC,

    Actually that is a better idea using the DTP. Question about the DTP though...how do you prevent the user from entering a date. I want the user to be able to click the drop down calendar ONLY as a source for getting the value.

    thx
    Blake

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Validating MaskedTextBox defined as a Date?

    Why would you want to limit your users to only be able to use the control 1 way when it works perfectly fine 2 ways?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] Validating MaskedTextBox defined as a Date?

    After playing around with it...I guess you can't enter an invalid date. So...problem solved altogether!

    Thx kleinma
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width