www.webdeveloper.com
Results 1 to 7 of 7

Thread: Problem with JavaScript regualr expression

  1. #1
    Join Date
    Nov 2013
    Posts
    72

    Problem with JavaScript regualr expression

    Can anyone point out the error is this regular expression? It is used for validating a date in the form of mm/dd/yyyy and for some reason, it will not allow me to enter a double digit for the day. I can enter 06 or 03 but not 15 or 22.


    Code:
    var reg = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;

  2. #2
    Join Date
    Mar 2011
    Posts
    61
    I'm lost on that one. Wouldn't it be easier to use the Date object?
    Code:
    function valiDate(date, compare){
     compare = new Date(date);
     date = date.split('/');
     return parseInt(date[0]) == compare.getMonth()+1 && parseInt(date[1]) == compare.getDate() && parseInt(date[2]) == compare.getFullYear();
    }
    
    validate('01/01/2014'); //true
    valiDate('02/29/2014'); //false
    valiDate('02/29/2012'); //true
    valiDate('13/01/2014'); //false

  3. #3
    Join Date
    Nov 2013
    Posts
    72
    Quote Originally Posted by Kever View Post
    I'm lost on that one. Wouldn't it be easier to use the Date object?
    Code:
    function valiDate(date, compare){
     compare = new Date(date);
     date = date.split('/');
     return parseInt(date[0]) == compare.getMonth()+1 && parseInt(date[1]) == compare.getDate() && parseInt(date[2]) == compare.getFullYear();
    }
    
    validate('01/01/2014'); //true
    valiDate('02/29/2014'); //false
    valiDate('02/29/2012'); //true
    valiDate('13/01/2014'); //false
    So I think I understand what you did there but why did you add one to getMonth()

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by Kever View Post
    I'm lost on that one. Wouldn't it be easier to use the Date object?
    Code:
    function valiDate(date, compare){
     compare = new Date(date);
     date = date.split('/');
     return parseInt(date[0]) == compare.getMonth()+1 && parseInt(date[1]) == compare.getDate() && parseInt(date[2]) == compare.getFullYear();
    }
    
    validate('01/01/2014'); //true
    valiDate('02/29/2014'); //false
    valiDate('02/29/2012'); //true
    valiDate('13/01/2014'); //false
    ...it's ok, but what will happen if user decide to input date in the format of mm-dd-yyyy? Another thing is that I don't think that the goal is to compare user input against current date.
    Last edited by tech_soul8; 03-15-2014 at 10:24 AM.

  5. #5
    Join Date
    Mar 2011
    Posts
    61
    It's because getMonth() returns the month from 0 to 11.

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    why not use <input type="date" /> instead?

  7. #7
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You can use regExp like this:

    Code:
    /^(\d{2,2}\/){2,2}\d{4,4}$/
    to force the user to input valid date format and then you can use some sort of the above function for any additional checkings.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles