Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Regex Date

  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regex Date

    hi all,
    can anyone give me regex for date validation in the format dd/mm/yyyy. it should consider all cases 30,31 days, leap years etc..
    it should validate present and future days only, past date should not be allowed.
    Any help would be appreciated.
    Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    A regex cannot handle those cases, it can only take care of general string content (with little to no logic)

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Actually, I have seen RegEx mask that will take all of that into consideration, but the string is a mile long.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Wow ok ... some people can do magic with regexes. But I also saw people turning a tv into a toaster :-)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    function checkValidDate(yr,mmx,dd) {
    
    // Is year sensible?
    
    if (yr <1910 || yr >2012) {  // you may want to change 2012 to some other year!
    alert ("Year is out of range")
    return false;
    }
    
    // Is the date a valid date?
    
    mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
    var nd = new Date();
    nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD
    
    var ndmm = nd.getMonth();
    if (ndmm != mm) {
    alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
    return false; 
    }
    else {
    alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
    }
    
    // Is the date today or after?
    
    var today = new Date();
    if (nd < today) {
    alert ("Date must be today or after");
    return false;
    }
    
    }
    
    // Usage
    checkValidDate(2013,2,31)  // 31st February 2013
    
    </script>

    Quizmaster: Charles who is said to be the father of modern electronic computing?
    Contestant: Darwin.
    Last edited by Philip M; 09-27-2012 at 04:28 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •