codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Regular expression - UK Date, UK Date & Time and a new e-mail one  Archive Import (James Laugesen) at 20:28 on Wednesday, July 23, 2003
 

I saw an article on validating a date in pure HTML, the author had written such complex javascript when a regular expression would work fine.
So I thought I`d post some ones I`ve written and find very handy.

-----
This validates UK dates in the format; dd/mm/yy,
dd/mm/yyyy, dd.mm.yy, dd.mm.yyyy (remember .`s should be replaced with /`s before going to a database). It also checks the number of days in each month and leap years. So 29/02/1983 would be invalid, 29/02/1984 would be valid.

^(??:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(??:29|30)(\\/|-|\\.)(?:0?[1,3-9]|1[0-2])\\2))(??:1[6-9]|[2-9]\\d)?\\d{4})$|^(?:29(\\/|-|\\.)0?2\\3(???:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(??:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\\\)(??:0?[1-9])|(?:1[0-2]))\\4(??:1[6-9]|[2-9]\\d)?\\d{4})$
-----
This validates the same as above, only it also checks for a valid time after the date.
So 12/2/2003 11:30,
12/2/2003 11.30,
12.2.2003 23.30,
12/2/2003 11.30pm,
etc, etc.
^((((31(\\/|-|\\.|\\\\)(0?[13578]|1[02]))|((29|30)(\\/|-|\\.|\\\\)(0?[1,3-9]|1[0-2])))(\\/|-|\\.|\\\\)(1[6-9]|[2-9]\\d)?\\d{2})|(29(\\/|-|\\.|\\\\)0?2(\\/|-|\\.|\\\\)(((1[6-9]|[2-9]\\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\\\)((0?[1-9])|(1[0-2]))(\\/|-|\\.|\\\\)((1[6-9]|[2-9]\\d)?\\d{2})) ((([0]?[1-9]|1[0-2])([0-5][0-9](([0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])([0-5][0-9](([0-5][0-9])?))$
-----
This validates e-mail addresses, although it`s still best to actively test an e-mail address by sending an e-mail.
As people pointed out here in something I posted an old e-mail expression on (1; it failed a lot, haha, hence this new one.) and 2; there can be tld and ip based addresses.
But I`d like to think someone dealing with users within a TLD or using IP based e-mails would use real methods to test the e-mail, and not just a regular expression.

Anyway, I`m just the messenger =8-)

^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$
-----

Cheers,
James

  eeek!!! now there`s a problem.  Archive Import (James Laugesen) at 20:33 on Wednesday, July 23, 2003
 

Codetoad guys; you really should have an options to dissable emoticons.
Here again...

----
DATE

^ ( ?: ( ?:31 ( \\/|-|\\. ) ( ?:0?[13578]|1[02] ) ) \\1| ( ?: ( ?:29|30 ) ( \\/|-|\\. ) ( ?:0?[1,3-9]|1[0-2] ) \\2 ) ) ( ?: ( ?:1[6-9]|[2-9]\\d ) ?\\d{4} ) $|^ ( ?:29 ( \\/|-|\\. ) 0?2\\3 ( ?: ( ?: ( ?:1[6-9]|[2-9]\\d ) ? ( ?:0[48]|[2468][048]|[13579][26] ) | ( ?: ( ?:16|[2468][048]|[3579][26] ) 00 ) ) ) ) $|^ ( ?:0?[1-9]|1\\d|2[0-8] ) ( \\/|-|\\.|\\\\ ) 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( ( ( [a-zA-Z0-9] ) + ( ( [\-] ) + ( [a-zA-Z0-9] ) + ) *\. ) + ( [a-zA-Z] ) + ( ( [\-] ) + ( [a-zA-Z0-9] ) + ) * ) ) $








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Digital.Canal.Software.V2013
•  Altair_HyperWorks_12.0.1_Win64
•  Global.mapper.V15.0
•  ESI.PAM-RTM.V2010.0
•  KISSsoft.03.2013E.SP5
•  Technical.Toolboxes.Pipeline.Toolbox.2013.Enterprise.v15.0.0
•  Simprotek.Simprosys.V3.0
•  Ricardo.SABR.V6.0p1
•  PSIM.V9.2


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2013