[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,
Re: [2005] Validating MaskedTextBox defined as a Date?
:wave:
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. :)
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.
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,
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
Re: [2005] Validating MaskedTextBox defined as a Date?
There's only one validation you should be doing:
vb.net Code:
Dim d As Date
If Date.TryParse(myMaskedTextBox.Text, d) Then
'd contains the value.
Else
'Invalid input.
End If
That said, why don't you just use a DateTimePicker?
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
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?
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