Solved

RegularExpression Validator for Date and number

Posted on 2004-08-16
7
280 Views
Last Modified: 2012-05-05
What is the regular expression for the reg validator for entering a date only as mm/dd/yyyy and for a textbox that requires up to 4 digits i.e. numbers only.

Thanks
0
Comment
Question by:dmontgom
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11811296
Date
 Expression:        

^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0
?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?
\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|
[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)
)))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8]
)\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[0
12])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}
)?$

      
Description:       DateTime Validator. This RE validates both dates and/or times patterns. Days in Feb. are also validated for Leap years. Dates: in the US mm/dd/yyyy or m/d/yy format between 1/1/1600 - 12/31/9999. Leading zeroes are optional. Date separators can be ...
Matches:       [12/25/2003], [08:03:31], [02/29/2004 12 AM]       [ More Details]
Non-Matches:       [02/29/2003 1:34 PM], [13:23 PM], [24:00:00]

 Expression:        

^[-+]?\d*$

Just set the max length field of the textbox to 4 and use this one:
      
Description:       Matches any integer number or numeric string, including positive and negative value characters (+ or -). Also matches empty strings.
Matches:       [123], [-123], [+123]       [ More Details]
Non-Matches:       [abc], [3.14159], [-3.14159]       [ Test Expression]
Rating:

Source:
http://www.regexlib.com/Default.aspx

Regards,

Aeros
0
 

Author Comment

by:dmontgom
ID: 11833147
What is the regular expression just for mm/dd/yyyy.  I want my date only in that form.

Thanks
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11833276
^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0
?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?
\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|
[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)
)))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8]
)\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[0
12])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}
)?$

DateTime Validator. This RE validates both dates and/or times patterns. Days in Feb. are also validated for Leap years. Dates: in the US mm/dd/yyyy or m/d/yy format between 1/1/1600 - 12/31/9999. Leading zeroes are optional. Date separators can be either matching dashes(-), slashes(/) or periods(.) Times: in the hh:MM:ss AM/PM 12 hour format (12:00 AM - 11:59:59 PM) or hh:MM:ss military time format (00:00:00 - 23:59:59). The 12 hour time format: 1) may have a leading zero for the hour. 2) Minutes and seconds are optional for the 12 hour format 3) AM or PM is required and case sensitive. Military time 1) must have a leading zero for all hours less than 10. 2) Minutes are manditory. 3) seconds are optional. Datetimes: combination of the above formats. A date first then a time separated by a space. ex) mm/dd/yyyy hh:MM:ss This RE is an extension of my Date validator already in this library, so the same rules for leap year apply. All 4 digit year and all two digit years except 00, which might not be a leap year. I'll also post the time validator separately.
Source:       Michael Ash

http://www.regexlib.com/REDetails.aspx?regexp_id=369

Regards,

Aeros
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Accepted Solution

by:
AerosSaga earned 50 total points
ID: 11833297
OR

}

To match a date in mm/dd/yyyy format, rearrange the regular expression to (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d. For dd-mm-yyyy format, use (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d.
0
 

Author Comment

by:dmontgom
ID: 11833607
Works great.  Thanks.  Just one more question, since the text box that contains the date, if I enter an invalid date, the message disapaers on post back.  How do I keep the message there on post back.  Textbox post property is set to true.

Thanks.  Only answer of you know off the top of your head.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11833713
hmm...it shouldn't but as a workaround you could take the errormessage.text and save it to a session variable, and then on postback assign the error.text the session variable which contains the orignal error message.

Regards

Aeros
0
 

Author Comment

by:dmontgom
ID: 11836436
I cannot beleive I cannot log into my own data base using microsoft products and I can from other products like sas.  Truly unbelievable
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now