Jump to content

Validate Date (dd.mm.yyyy)


Recommended Posts

This regex isn't ridiculous.

It's simply plain wrong and on two points:

  1) days 19 of every MM.YYYY are flagged invalid

  2) any year multiple of 100 but not 400 is wrongly considered leap (e.g. 1900, 2100, 2200, ...).

Fixing 1) is easy: test for 29.02 before the rest and extend the day range to include 19.

Fixing 2) is more cumbersome (and is left as exercise to the reader).

I highly recommend using (?x) to allow breaking down the expression into more easily understandable small parts.

Rather that an error-prone one liner, I suggest writing it like that (this version not fixed!):

Func _Valid($val)
   Return StringRegExp($val, _
'(?x)                                                   ' & _   ; allow meaningless whitespaces in expression to ease reading
'   ^                                                   ' & _   ; start of subject string
'       (                                               ' & _   ; all DD.MM dates except 29-02s in years [1900, 9999]
'           (                                           ' & _   ;   DD.MM not 29.02
'               (                                       ' & _   ;       DD in [1, 9] ∪ [10, 18] ∪ [20, 28] but note that 19 is missing
'                   ( 0 [1-9] | [12] [0-8] )            ' & _   ;           DD in this range
'                   \.                                  ' & _   ;           dot before MM
'                   ( 0 [1-9] | 1 [012] )               ' & _   ;           MM in [1, 9] ∪ [10, 12], that is [1, 12]
'               )                                       ' & _   ;       end
'               |                                       ' & _   ;       or
'               (                                       ' & _   ;       DD in [29, 31] and MM in {1, 3, 5, 7, 8, 10, 12}
'                   ( 29 | 30 | 31 )                    ' & _   ;           DD in this range
'                   \.                                  ' & _   ;           dot before MM
'                   ( 0 [13578] | 1 [02] )              ' & _   ;           MM in this range
'               )                                       ' & _   ;       end
'               |                                       ' & _   ;       or
'               (                                       ' & _   ;       DD in [29, 30] and MM in {4, 6, 9, 11}
'                   ( 29 | 30 )                         ' & _   ;           DD in this range
'                   \.                                  ' & _   ;           dot before MM
'                   ( 0 [469] | 11 )                    ' & _   ;           MM in this range
'               )                                       ' & _   ;       end
'           )                                           ' & _   ;   end
'           \.                                          ' & _   ;   dot before year
'           ( 19 | [2-9] \d )                           ' & _   ;   19th and [20..99]th centuries
'           \d\d                                        ' & _   ;   year within century [0, 99]
'       )                                               ' & _   ;
'       |                                               ' & _   ; or
'       (                                               ' & _   ;   all 29-02s in years [1900, 9999]
'           29 \. 02 \. ( 19 | [2-9] \d )               ' & _   ;       29.02 in 19th or [20, 99]th century
'           ( 00 | 04 | 08 | 12 | 16 | 20 | 24 | 28 |   ' & _   ;       year
'             32 | 36 | 40 | 44 | 48 | 52 | 56 | 60 |   ' & _   ;       within
'             64 | 68 | 72 | 76 | 80 | 84 | 88 | 92 |   ' & _   ;       century
'             96 )                                      ' & _   ;       but note that 1900 is wrongly considered leap
'       )                                               ' & _   ;   end
'   $                                                   ' _     ; end of subject string
)
EndFunc

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Here's my try:

#include <Date.au3>

Local $dates = [ _
    '19.03.2015', _
    '32.03.2015', _
    '29.02.2015', _
    '29.02.1900', _
    '29.02.2100', _
    '29.02.2016', _
    '29.02.2000', _
    '31.12.2015' _
]

For $s In $dates
    ConsoleWrite($s & ' is ' & (_Valid($s) ? '' : 'in') & 'valid: ' & ((_Valid($s) = _IsThisValidDate($s)) ? 'true' : 'false') & @LF)
Next

Func _Valid($val)
   Return StringRegExp($val, _
'(?x)                                                   ' & _   ; allow meaningless whitespaces in expression to ease reading
'   ^                                                   ' & _   ; start of subject string
'       (                                               ' & _   ;   all 29-02s in years [1900, 9999]
'           29 \. 02 \. ( 19 | [2-9] \d )               ' & _   ;       29.02 in 19th or [20, 99]th century
'           (                                           ' & _   ;       but only for valid leap years
'               (?<= 20 | 24 | 28 | 32 | 36 | 40 | 44 | ' & _   ;           YYYY ≡ 0 (mod 400) are leap
'                    48 | 52 | 56 | 60 | 64 | 68 | 72 | ' & _   ;           otherwise YYYY ≡ 0 (mod 100) are not
'                    76 | 80 | 84 | 88 | 92 | 96) 00 |  ' & _   ;           years 0 of centuries ≡ 0 (mod 400)
'             04 | 08 | 12 | 16 | 20 | 24 | 28 | 32 |   ' & _   ;       "normal" leap years
'             36 | 40 | 44 | 48 | 52 | 56 | 60 | 64 |   ' & _   ;       within
'             68 | 72 | 76 | 80 | 84 | 88 | 92 | 96 )   ' & _   ;       any century
'       )                                               ' & _   ;   end
'       |                                               ' & _   ; or
'       (                                               ' & _   ; all DD.MM dates except 29-02s in years [1900, 9999]
'           (                                           ' & _   ;   DD.MM not 29.02
'               (                                       ' & _   ;       DD in [1, 9] ∪ [10, 19] ∪ [20, 28]
'                   ( 0 [1-9] | 1 [0-9] | 2 [0-8] )     ' & _   ;           DD in this range
'                   \.                                  ' & _   ;           dot before MM
'                   ( 0 [1-9] | 1 [012] )               ' & _   ;           MM in [1, 9] ∪ [10, 12], that is [1, 12]
'               )                                       ' & _   ;       end
'               |                                       ' & _   ;       or
'               (                                       ' & _   ;       DD in [29, 31] and MM in {1, 3, 5, 7, 8, 10, 12}
'                   ( 29 | 30 | 31 )                    ' & _   ;           DD in this range
'                   \.                                  ' & _   ;           dot before MM
'                   ( 0 [13578] | 1 [02] )              ' & _   ;           MM in this range
'               )                                       ' & _   ;       end
'               |                                       ' & _   ;       or
'               (                                       ' & _   ;       DD in [29, 30] and MM in {4, 6, 9, 11}
'                   ( 29 | 30 )                         ' & _   ;           DD in this range
'                   \.                                  ' & _   ;           dot before MM
'                   ( 0 [469] | 11 )                    ' & _   ;           MM in this range
'               )                                       ' & _   ;       end
'           )                                           ' & _   ;   end
'           \.                                          ' & _   ;   dot before year
'           ( 19 | [2-9] \d )                           ' & _   ;   19th and [20..99]th centuries
'           \d\d                                        ' & _   ;   year within century [0, 99]
'       )                                               ' & _   ;
'   $                                                   ' _     ; end of subject string
)
EndFunc

; much preferable
Func _IsThisValidDate($str)
    Return _DateIsValid(StringRegExpReplace($str, "^(\d\d).(\d\d).(\d\d\d\d)$", "$3-$2-$1"))
EndFunc

 

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...