Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Validating Birthday on a Textbox

Posted on 2004-08-23
7
Medium Priority
?
965 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:Trancedified
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 560 total points
ID: 11875859
The example you gave:

    08/23/2004 (today)

is in the format:

    mm/dd/yyyy

but your code is checking for:

    dd/mm/yyyy

Also, you want to know when the format DOES NOT match what have...
Try this instead:

    Private Sub txtDOB_Validate(Cancel As Boolean)
        If txtDOB.Text <> Format(txtDOB.Text, "mm/dd/yyyy") Then
            MsgBox "The Date of Birth (DOB) must be of this format: " + "mm/dd/yyyy.", vbInformation, "Validation"
        End If
    End Sub

Regards,

Idle_Mind
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11875869
Well, the first thing is that you are showing the message box if it DOES match!
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 800 total points
ID: 11875887
You can make a more intensive test, something like:

Private Sub txtDOB_Validate(Cancel As Boolean)
    Dim valid as Boolean
    valid = True

    If Mid(txtDOB.Text,3,1) <> "/" Or Mid(txtDOB.Text,6,1) <> "/" Then
          valid = False
    Else
          day = val(Mid(txtDOB.Text,1,2))
          mon = val(Mid(textDOB.Text,4,2))
          year = val(Mid(textDOB.Text,7))

          If day<1 Or day>31 Or mon<1 Or mon>12 or year<1900 or year>2004 Then valid = False
    End If

    If Not valid Then
        MsgBox "The Date of Birth (DOB) must be of this format: " + "dd/mm/yyyy.", vbInformation, "Validation"
    End If

End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11875913
I see that Idle_Mind has the other issue as well.
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 640 total points
ID: 11875939
1) Use the IsDate function to check if it is a valid date.
2) Then use the CDate function to convert the text to a date.
3) Convert the date field to the required format using the format function

This will mean thy can enter the date in any format they want includeing for example "24 Aug 1973"

Dim dDOB as Date
If IsDate(Text1.Text) Then
    dDOB = CDate(Text1.Text)
    Text1.Text = Format(dDOB, "dd/mm/yyyy")
Else
    MsgBox "The Date of Birth (DOB) must be of this format: " & "dd/mm/yyyy.", vbInformation, "Validation"
End If

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11875947
So many ways to skin a cat...

=)
Idle_Mind
0
 
LVL 1

Author Comment

by:Trancedified
ID: 11876004
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


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question