Trancedified
asked on
Validating Birthday on a Textbox
Helllo,
I have a question about validating a birthday which will be inputted in a textbox. It's supposed to look like:
08/23/2004 (today)
I have this code below, but for some reason it won't accept the same month/day. For example, if I input 10/10/1979 or 05/05/1922. The message box shows up if I go through it:
'Validating Date of Birth so the format should be Year-Month-Day
Private Sub txtDOB_Validate(Cancel As Boolean)
If txtDOB.Text = Format(txtDOB.Text, "dd/MM/yyyy") Then
MsgBox "The Date of Birth (DOB) must be of this format: " + "dd/mm/yyyy.", vbInformation, "Validation"
End If
End Sub
Any suggestions?
Chris
I have a question about validating a birthday which will be inputted in a textbox. It's supposed to look like:
08/23/2004 (today)
I have this code below, but for some reason it won't accept the same month/day. For example, if I input 10/10/1979 or 05/05/1922. The message box shows up if I go through it:
'Validating Date of Birth so the format should be Year-Month-Day
Private Sub txtDOB_Validate(Cancel As Boolean)
If txtDOB.Text = Format(txtDOB.Text, "dd/MM/yyyy") Then
MsgBox "The Date of Birth (DOB) must be of this format: " + "dd/mm/yyyy.", vbInformation, "Validation"
End If
End Sub
Any suggestions?
Chris
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, the first thing is that you are showing the message box if it DOES match!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I see that Idle_Mind has the other issue as well.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So many ways to skin a cat...
=)
Idle_Mind
=)
Idle_Mind
ASKER
Oops yeah I made a mistake, it's suppossed to be mm/dd/yyyy (I'm fired) :)
JR2003, yours works corrected:
Private Sub txtDOB_Validate(Cancel As Boolean)
Dim dDOB As Date
If IsDate(txtDOB.Text) Then
dDOB = CDate(txtDOB.Text)
txtDOB.Text = Format(dDOB, "mm/dd/yyyy")
Else
MsgBox "The Date of Birth (DOB) must be of this format: " & "mm/dd/yyyy.", vbInformation, "Validation"
End If
End Sub
jaime_olivares, works great too, and has some extra lines of code to find out if it's formatted correctly:
Private Sub txtDOB_Validate(Cancel As Boolean)
Dim valid As Boolean
valid = True
Dim mon, day, year As String
If Mid(txtDOB.Text, 3, 1) <> "/" Or Mid(txtDOB.Text, 6, 1) <> "/" Then
valid = False
Else
mon = Val(Mid(txtDOB.Text, 1, 2))
day = Val(Mid(txtDOB.Text, 4, 2))
year = Val(Mid(txtDOB.Text, 7))
If day < 1 Or day > 31 Or mon < 1 Or mon > 12 Or year < 1900 Then valid = False
End If
If Not valid Then
MsgBox "The Date of Birth (DOB) must be of this format: " + "mm/dd/yyyy.", vbInformation, "Validation"
End If
End Sub
Idle_Mind, yours too!
Chris
JR2003, yours works corrected:
Private Sub txtDOB_Validate(Cancel As Boolean)
Dim dDOB As Date
If IsDate(txtDOB.Text) Then
dDOB = CDate(txtDOB.Text)
txtDOB.Text = Format(dDOB, "mm/dd/yyyy")
Else
MsgBox "The Date of Birth (DOB) must be of this format: " & "mm/dd/yyyy.", vbInformation, "Validation"
End If
End Sub
jaime_olivares, works great too, and has some extra lines of code to find out if it's formatted correctly:
Private Sub txtDOB_Validate(Cancel As Boolean)
Dim valid As Boolean
valid = True
Dim mon, day, year As String
If Mid(txtDOB.Text, 3, 1) <> "/" Or Mid(txtDOB.Text, 6, 1) <> "/" Then
valid = False
Else
mon = Val(Mid(txtDOB.Text, 1, 2))
day = Val(Mid(txtDOB.Text, 4, 2))
year = Val(Mid(txtDOB.Text, 7))
If day < 1 Or day > 31 Or mon < 1 Or mon > 12 Or year < 1900 Then valid = False
End If
If Not valid Then
MsgBox "The Date of Birth (DOB) must be of this format: " + "mm/dd/yyyy.", vbInformation, "Validation"
End If
End Sub
Idle_Mind, yours too!
Chris