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
Solved

RegularExpression Validator for Date and number

Posted on 2004-08-16
7
284 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…

861 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