Solved

RegularExpression Validator for Date and number

Posted on 2004-08-16
7
279 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to Popup 37 79
crm written in asp.net / c# 1 42
Binding a check box in an Asp.net DataGrid 4 26
ASP.NET reading ATOM 2 26
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 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

18 Experts available now in Live!

Get 1:1 Help Now