Validate textstring as DateTime

Hello,

Can this be done in VB2005Xpress?

Form elements: one TextBox, one Label (TextBox is used to receive Birthdate input, Label displays "<1", "1" "the age which is calculated from Today's Date - Birthdate" or "?"). The age formula and the If statement for "<1", "1", "age" and "?" is done. Here the TextBox is the choice for Birthdate entry. It is the most cost effective. DatePicker is excellent choosing a future date where the user want to be sure that the Day picked out do not fall for example on Sunday. Click event should be not necessary to display the result in the Label.

Birthdate correct form is: ddMMyyyy

Cases:

1. User type letters (probably did not pay attention that it was BirthDate field) > it is ignored

2. User type 2/28/ "?" is displayed

3. User type 28/12/2008 "?" is displayed

4. 28/12/2 is also interpreted as 28/12/2002

5. 28/12/20 "?" is displayed because of the ambiguity (1920 or 2020)

Kind regards

--

This newbie can't speak and can't walk. Embarrassing.

[1643 byte] By [CesarFrancisco] at [2007-12-21]
# 1

Sure you can do it.

But you are going to be writing a considerable amount of code in order to parse the text on every keystroke and have to break down the currently entered textbox contents into there determine date components (d/m/y) and validate each as well as update the contents of the textbox date based upon those items. Also you'd need to make it sufficiently robust to handle different date formats.

Its definately possible, would I want to write such code - no I'd either use the datepicker as advise previously, simply validate the date as a whole and not while they are typing, use a masked edit control or search for an existing date control from a 3rd party in order to achieve the said result - although I fear that most of the date entry controls are going to be similar to textbox, masked edit and datapicker.

spotty at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

How difficult this would be to write depends on how stuck to those rules you are. For instance, here is a short piece of code that achieves most of your goals.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

Dim dt As Date
If Date.TryParse(TextBox1.Text, dt) Then

Dim span As TimeSpan = Now.Subtract(dt)
If span.TotalDays < 365 Then

Label1.Text = "<1"

Else

Label1.Text = CInt(span.TotalDays / 365)

End If

Else

Label1.Text = "?"

End If

End Sub

Letters are not being "ignored" but you could add a little routine to remove them from the textbox before trying the date.

The specific format that is considered acceptable is determined by the windows region settings. So if you computer is set for the ddMMyyyy date style then that is how it will be tested by the TryParse() method. This is how date formats should be handled - let the user decide what format will run on their PC.

HTH!

rkimble at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Here, this follows all the rules, removing letters allowing spaces, forward slashes, and dashes and displaying the proper output of <1, 1, age, or ?. The user still has to enter the date in the same format as the Windows region settings:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

Dim sb As New System.Text.StringBuilder
Dim pos As Integer = TextBox1.SelectionStart

For Each c As Char In TextBox1.Text
Select Case c
Case " ", "/", "-"
sb.Append(c)
Case Else
If Char.IsNumber(c) Then
sb.Append(c)
End If
End Select
Next
TextBox1.Text = sb.ToString
TextBox1.SelectionStart = pos

Dim dt As Date
If Date.TryParse(TextBox1.Text, dt) Then
Dim span As TimeSpan = Now.Subtract(dt)
If span.TotalDays < 1 Then
Label1.Text = "?"
ElseIf span.TotalDays < 365 Then
Label1.Text = "<1"
Else
Label1.Text = CInt(span.TotalDays / 365)
End If
Else
Label1.Text = "?"
End If

End Sub

rkimble at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Hello rkimble,

Who is this guy? Code: short, elegant and fast. Wow! Thank you keeping the hope alive that actually something worthwile can be achieved in VB.

After spending two entirely futil days in the company of "Case" "if" to validate the Birthdate text - was the solution so easy? Which bring up this ...> typical newbie question: Where can you get all this information: "As Char In", "Append ()", "IsNumber", "StringBuilder","SelectionStart", "TryParse", "NowSubtract".

Thank you genius,

Kind regards.

This newbie can't talk and can't walk. Embarrassing.

CesarFrancisco at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

lol

Well, thank you, but there's nothing genius about it. If you want to be aware of the kinds of things you mentioned, you've really got three choices: go to school and have someone teach you all these nifty things, buy books and read 'em (probably lots of books!), or, do what I did and just experiment and practice (using a lot of F1 and the forums for the really tough stuff).

Remember that the framework is very robust and powerful. On average, the sorts of things a programmer needs to do are wrapped up in a method somewhere. For example, in wanting to manipulate a date in any way, first create a variable of Type Date, type the variable's name, hit ".", and look through the intellisense for a property or method that may help you. F1 anything that looks promising. Experiment and read help files. Look at example code. I've picked up all kinds of "I didn't know you could do that!"s from looking at example code.

But in the end it's all about experience. I've been messing with this .NET stuff since shortly after it's release. I was just really getting the hang of VB6 at the time. Before that I coded in BASIC in most of it's forms from the C64 to the Apple II and onto the X86 (runtime and all). Basic has been a hobby of mine since I was 10.

So just keep at it. Struggling for days on problomatic, monolithic, code and then getting an answer that's less than 30 lines is part of the learning process. But now you've got a list of objects and methods to read up on (F1!! make good, good friends with the MSDN library) and those will lead to many, many more. Take your time and practice, practice, practice!

rkimble at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...