Solved

Date input validation (Regular Expressions) [2]

Posted on 2016-11-27
4
90 Views
Last Modified: 2016-11-27
I had this question after viewing Date input validation (Regular Expressions).

Hi guys,
I have expression below :
^((0?[1-9]|[12][0-9]|3[01])[\/](0?[13578]|1[02])[\/](18|19|20)[0-9]{2})|((0?[1-9]|[12][0-9]|30)[\/](0?[469]|11)[\/](18|19|20)[0-9]{2})|((0[1-9]|1[0-9]|2[0-8])[\/](0?[2])[\/](18|19|20)[0-9]{2})|(29[\/](0?[2])[\/](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$

Open in new window


1. Day validation of 28, 29, 30, 31
2. years in range 1800-2099
3. seperator /
4. format dd/mm/yyyy

I tied to fix days and months in range   1, 2, 3, 4, . . .  or  01, 02, 03, 04, . . .

validation is successful on any month and any year (include leap year)
Except these entries on February: 1/2/2016, 2/02/2016, 3/2/2017 . . .  Failure on one digit day.

 Thank you!
0
Comment
Question by:Darius
[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
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 41903389
Looks like you just left out the ? after 0 day in February, line 6 in the Perl example below
my $a = '2/02/2016';

if ($a =~ /^
    ((0?[1-9]|[12][0-9]|3[01])[\/](0?[13578]|1[02])[\/](18|19|20)[0-9]{2})|  # jan, march, may etc
    ((0?[1-9]|[12][0-9]|30)[\/](0?[469]|11)[\/](18|19|20)[0-9]{2})|          # apr, jun etc
    ((0?[1-9]|1[0-9]|2[0-8])[\/](0?[2])[\/](18|19|20)[0-9]{2})|              # feb
    (29[\/](0?[2])[\/](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$   # leap day
    /x )
{
    print "matched day: $1\n";
}

Open in new window


There are plenty of lubraries to do this type of validation: Date::Parse
0
 

Author Comment

by:Darius
ID: 41903399
Hi Kim,

  I have the same changes done, but it brake February validation

For example:  29/2/2017,  29/02/2017,  29/2/2018,  29/02/2018, 29/2/2019,  29/02/2019, . . .

Leap year 2016, 2020, . . .  correct!

Thanks
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 500 total points
ID: 41903459
The anchors point ^ and $ didn't seem to be working, they need to be inside brackets.

    ((^0?[1-9]|[12][0-9]|3[01])[\/](0?[13578]|1[02])[\/](18|19|20)[0-9]{2})$|  # jan, march, may etc
    ((^0?[1-9]|[12][0-9]|30)[\/](0?[469]|11)[\/](18|19|20)[0-9]{2})$|          # apr, jun etc
    ((^0?[1-9]|1[0-9]|2[0-8])[\/](0?[2])[\/](18|19|20)[0-9]{2})$|              # feb
    (^29[\/](0?[2])[\/]
     ((
       (18|19|20)(04|08|
        [2468][048]|[13579][26]))|
        2000
    $))   # leap day

Open in new window



This would be a lot simpler if you just extracted dd, mm, yyyy and then used if then logic to validate all cases (such as is year divisible by 4 but not 400 etc).
1
 

Author Comment

by:Darius
ID: 41903468
Kim,

This is just a challenge for myself. Thanks for help!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

617 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