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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2009
    Posts
    121
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Regular Expression Help.

    Hi,
    Can someone fix the following regular expression? It is validating every thing expect YEAR. It only matches first 2 digits for year instead of 4. I want it to be 4 digits for a year. It returns true if I put 12/25/19.I want it to return false for the above date.

    Code:
    var RegExPattern = /^(?=\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[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    Thanks
    Last edited by asifakhtar; 06-14-2010 at 03:31 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    Is this what you are trying to achieve?
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function checkDate( fld )
    {
        var jmmjaod = "(0?[13578]|10|12)\\/(0?[1-9]|[12]\\d|30|31)\\/\\d{4}";
        var ajsn    = "(0?[469]|11)\\/(0?[1-9]|[12]\\d|30)\\/\\d{4}";
        var f       = "0?2\\/(0?[1-9]|1\\d|2[0-8])\\/((17|18|19|20)([02468][1235679]|[13579][01345789])|(1700|1800|1900))";
        var fly     = "0?2\\/(0?[1-9]|1\\d|2[0-9])\\/((17|18|19|20)([2468][048]|[13579][26]|04|08)|2000)";
        var all     = "^(\n" + jmmjaod + "|\n" + ajsn + "|\n" + f + "|\n" + fly + "\n)$";
        var re      = new RegExp("^(" + jmmjaod + "|" + ajsn + "|" + f + "|" + fly + ")$", "" );
        if ( re.test( fld.value ) ) alert( "valid date" );
        else                        alert( "invalid date" );
    }
    </script>
    </head>
    <body>
    <form>
    enter mm/dd/yyyy: <input name="theDate"/>
    <br/>
    <input type="button" value="check" onclick="checkDate(this.form.theDate)"/>
    </form>
    </body>
    </html>
    Not at all the same RE as you are using, but this one works, if you care.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    If you are trying to check whether a given date is valid or not,

    Code:
    <script type = "text/javascript">
    
    function checkValidDate(yr,mmx,dd) {
    
    if (yr <1990 || yr >2010) {  // you may want to change 2010 to some other year!
    alert ("Invalid year!")
    return false;
    }
    
    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");
    }
    }
    
    checkValidDate(2010,2,31)  // 31st February 2010
    </script>
    This works for leap years as well.

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Posts
    121
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Is this what you are trying to achieve?
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function checkDate( fld )
    {
        var jmmjaod = "(0?[13578]|10|12)\\/(0?[1-9]|[12]\\d|30|31)\\/\\d{4}";
        var ajsn    = "(0?[469]|11)\\/(0?[1-9]|[12]\\d|30)\\/\\d{4}";
        var f       = "0?2\\/(0?[1-9]|1\\d|2[0-8])\\/((17|18|19|20)([02468][1235679]|[13579][01345789])|(1700|1800|1900))";
        var fly     = "0?2\\/(0?[1-9]|1\\d|2[0-9])\\/((17|18|19|20)([2468][048]|[13579][26]|04|08)|2000)";
        var all     = "^(\n" + jmmjaod + "|\n" + ajsn + "|\n" + f + "|\n" + fly + "\n)$";
        var re      = new RegExp("^(" + jmmjaod + "|" + ajsn + "|" + f + "|" + fly + ")$", "" );
        if ( re.test( fld.value ) ) alert( "valid date" );
        else                        alert( "invalid date" );
    }
    </script>
    </head>
    <body>
    <form>
    enter mm/dd/yyyy: <input name="theDate"/>
    <br/>
    <input type="button" value="check" onclick="checkDate(this.form.theDate)"/>
    </form>
    </body>
    </html>
    Not at all the same RE as you are using, but this one works, if you care.
    Thank you for your reply but I want 2 digits for MONTH, 2 digits for DAY and 4 digits for YEAR with leap year support.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    I want 2 digits for MONTH, 2 digits for DAY and 4 digits for YEAR with leap year support.
    I gave you all of that except for *insisting* on the 2 digit months and days.

    But all you had to do require them was remove the ? from each of the 0? combinations:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function checkDate( fld )
    {
        var jmmjaod = "(0[13578]|10|12)\\/(0[1-9]|[12]\\d|30|31)\\/\\d{4}";
        var ajsn    = "(0[469]|11)\\/(0[1-9]|[12]\\d|30)\\/\\d{4}";
        var f       = "02\\/(0[1-9]|1\\d|2[0-8])\\/((17|18|19|20)([02468][1235679]|[13579][01345789])|(1700|1800|1900))";
        var fly     = "02\\/(0[1-9]|1\\d|2[0-9])\\/((17|18|19|20)([2468][048]|[13579][26]|04|08)|2000)";
        var re      = new RegExp("^(" + jmmjaod + "|" + ajsn + "|" + f + "|" + fly + ")$", "" );
        if ( re.test( fld.value ) ) alert( "valid date" );
        else                        alert( "invalid date" );
    }
    </script>
    </head>
    <body>
    <form>
    enter mm/dd/yyyy: <input name="theDate"/>
    <br/>
    <input type="button" value="check" onclick="checkDate(this.form.theDate)"/>
    </form>
    </body>
    Oh, and I limit the years to 1700 through 2099 for the month of February. I should probably have done the same for the other months. (No point in going back before 1653. The Gregorian calendar didn't exist before then. Could easily extend to more centuries in the future if you wanted. But, really, Philip's kind of solution is so much easier in JavaScript. You can code it even simpler than he did, if you want.]

    Oh, what the heck. 1700 through 2099 for all months:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function checkDate( fld )
    {
        var jmmjaod = "(0[13578]|10|12)\\/(0[1-9]|[12]\\d|30|31)\\/(17|18|19|20)\\d\\d";
        var ajsn    = "(0[469]|11)\\/(0[1-9]|[12]\\d|30)\\/(17|18|19|20)\\d\\d";
        var f       = "02\\/(0[1-9]|1\\d|2[0-8])\\/((17|18|19|20)([02468][1235679]|[13579][01345789])|(1700|1800|1900))";
        var fly     = "02\\/(0[1-9]|1\\d|2[0-9])\\/((17|18|19|20)([2468][048]|[13579][26]|04|08)|2000)";
        var re      = new RegExp("^(" + jmmjaod + "|" + ajsn + "|" + f + "|" + fly + ")$", "" );
        if ( re.test( fld.value ) ) alert( "valid date" );
        else                        alert( "invalid date" );
    }
    </script>
    </head>
    <body>
    <form>
    enter mm/dd/yyyy: <input name="theDate"/>
    <br/>
    <input type="button" value="check" onclick="checkDate(this.form.theDate)"/>
    </form>
    </body>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    If you want to insist on 2 digit days and months (why?? what difference does that make???), I'd use something like this:

    Code:
    function isValidDate( value )
    {
        var re = /^\d\d\/\d\d\/\d\d\d\d$/;
        if ( ! re.test( value ) ) return false;
        var parts = value.split("/");
        var mn = parseInt(parts[0])-1;
        var dt = new Date( parseInt(parts[2]), mn, parseInt(parts[1]));
        return mn == dt.getMonth();
    }
    Don't see how you can get much simpler than that.

    If you want to allow single digit months/days just change the re:
    Code:
        var re = /^\d?\d\/\d?\d\/\d\d\d\d$/;
    And the best part is you can change it to accept European dates (dd/mm/yyyy) by just changing two lines:
    Code:
        var mn = parseInt(parts[1])-1;
        var dt = new Date( parseInt(parts[2]), mn, parseInt(parts[0]));
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Feb 2009
    Posts
    121
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    If you want to insist on 2 digit days and months (why?? what difference does that make???), I'd use something like this:

    Code:
    function isValidDate( value )
    {
        var re = /^\d\d\/\d\d\/\d\d\d\d$/;
        if ( ! re.test( value ) ) return false;
        var parts = value.split("/");
        var mn = parseInt(parts[0])-1;
        var dt = new Date( parseInt(parts[2]), mn, parseInt(parts[1]));
        return mn == dt.getMonth();
    }
    Don't see how you can get much simpler than that.

    If you want to allow single digit months/days just change the re:
    Code:
        var re = /^\d?\d\/\d?\d\/\d\d\d\d$/;
    And the best part is you can change it to accept European dates (dd/mm/yyyy) by just changing two lines:
    Code:
        var mn = parseInt(parts[1])-1;
        var dt = new Date( parseInt(parts[2]), mn, parseInt(parts[0]));
    Thank u v much. U R the man. Thanks.


  •  

    Posting Permissions

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