?
Solved

regular expression for date

Posted on 2003-11-12
12
Medium Priority
?
903 Views
Last Modified: 2008-02-01
Hi,

              Could anyone  provide me a regular expression for  date in MMDDYYYY format?....

thank you
0
Comment
Question by:sunny1510
[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
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9731596
Can you explain please?
0
 

Author Comment

by:sunny1510
ID: 9731655
I am validating a date entered in a text box.....The user is only allowed to enter the date in mmddyyyy format......i know checking the format is easy with regex .......but i want the regular expression to check if it is a valid date.....
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9731720
Well if in your Glabal Region Settings if your Setting is set to mmddyyyy then just use IsDate()
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:sunny1510
ID: 9731770
Thanks for the response  Milka..The date is being validated when the textbox loses focus.....for certain date fields i also allow default value of 00000000 or a blank field. In that case this isdate() will give me an error. That is y i thought i will use regex.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9731792
so

If txtDAte.text = "" or txtDAte.text  = "00000000"  then
else
    if not isdate(txtdate.text) then Msgbox "Wrong Date"
end if

 
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9731820
rahter than requiring the user to enter the date as a sequence of digits (by the way, since the entry IS a sequence of digits, a regex will not help, since any digit is valid, but you want to be sure that they are no just digits, but also make up a valid date, and that is NOT what regex is used for), why do you not prenet the user with a Calendar control, and they you are CERTAIN they have picked a valid date?

AW
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9731837
Here is an example. I think you will find it helpful

 ' The following pattern checks whether the input string is a date in the
        ' format mm-dd-yy or mm-dd-yyyy. Key pattern elements used are:
        '
        '   \d{1,2}     Month and day numbers can have 1 or 2 digits. The use of
        '               (\d{4}|\d{2}) means the year can have 2 or 4 digits.
        '   (/|-)       Either the slash or the dash are valid date separators.
        '   \1          The separator used for the day and year must be the same
        '               as the separator used for month and day. The 1 refers to the
        '               first numbered group, defined by parentheses, e.g, (/|-).
        '  
        ' You could improve on this pattern by ensuring that digits do not start with
        ' a zero and that they are in a valid numerical range.
        '
        If Not Regex.IsMatch(txtDate.Text, _
            "^\s*\d{1,2}(/|-)\d{1,2}\1(\d{4}|\d{2})\s*$") Then

            txtDate.ForeColor = Color.Red
            IsValid = False
        Else
            txtDate.ForeColor = Color.Black
        End If
0
 

Author Comment

by:sunny1510
ID: 9731877
I have a number of text boxes in my app.....what if there is  a situation where one of these will accepts a value of "" and the other other textbox  needs a valid date .... In that case if the user enters a "" and exits... the isdate() fuction will accept it.
0
 

Author Comment

by:sunny1510
ID: 9731961
Hi all,

       Here i found a link where they have used regular expression for validating dates of mm/ddy/yyyy format.I am trying to use this regex since this appliction is a data entry program and i would prefer the user keying it in a text box.....Also I cannot write one function which will handle all the date validations since every date-text box has a  different default value......

http://www.regxlib.com/REDetails.aspx?regexp_id=279

please let me know if there is any other way of doing this...... thanks
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9732154
why dont you use a masked Edit control

you can set your MASK?
0
 
LVL 9

Expert Comment

by:malharone
ID: 9733127
i'll recommend what i've always recommended for regex.
great for building & testing
http://www.codeproject.com/dotnet/expresso.asp?target=expresso
(?<Month>\d{1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))(?x)

it includes a predefined regex for data.
however it'll NOT check for proper 'date value' given the month number.
so you could enter 4/31/2001 and it'll accept it.
So what you'd need to do is call
dim yourdatestring as string="4/31/2001"
if isDate(yourDateString)=false then
  messagebox.show(yourdateString & " is not a valid date")
else
  'all is OK
end if
0
 
LVL 9

Accepted Solution

by:
malharone earned 280 total points
ID: 9733148
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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