Solved

RegularExpression Validator for Date and number

Posted on 2004-08-16
7
282 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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