Solved

RegularExpression Validator for Date and number

Posted on 2004-08-16
7
288 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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