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

x
?
Solved

regular expression for date

Posted on 2003-11-12
12
Medium Priority
?
908 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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