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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    190
    Thanks
    143
    Thanked 0 Times in 0 Posts

    Date Validator Similar to isDate()

    Hi There,

    I really need your help.

    I am aware that there is no isDate() function for javascript, however, how could one check a date against a validator to ensure that the date entered is either returned true or false

    the date format that I would like to check would consist of the dd/mm/yyyy

    so essentially:

    isDate("12/01/2012") would return as true

    isDate("N/A") = would return as false

    I have searched high and low and am counting on the experts on this forum for some major help.

    A huge thanks in advance and I really appreciate everything.

    Cheers,

    Jay

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    you can check that the date is in the format you want and that there are no absurd numbers (like month 13 or day 65) like this:

    Code:
    var d =document.getElementById('txt').value
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/;
    if (!validformat.test(d)){
    alert('please enter your date of birth in dd/mm/yyyy format')
    return;
    }
    var day=d.split("/")[0]
    var mth=d.split("/")[1]
    var yr=d.split("/")[2]
    var bday = new Date(yr, mth-1, day)
    if ((bday.getMonth()+1!=mth)||(bday.getDate()!=day)||(bday.getFullYear()!=yr)) {
    alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
    return;
    }

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts
    Nice check. I modified it to reduce the number of variables and add an alternate input format.
    Code:
    <script type="text/javascript">
    function validDate() {
      var ErrCnt = -1;
      var ErrMsg = ['Please enter your date of birth in dd/mm/yyyy format',
                    'Invalid Day, Month, or Year range detected. Please correct and submit again.'];
      var dt = document.getElementById('txt').value;
      var validformat = /^\d{2}[\/\-]\d{2}[\/|\-]\d{4}$/;
      if (!validformat.test(dt)){ ErrCnt = 0; }
      var d = dt.split(/[\/|\-]/);
      var bday = new Date(d[2],(d[1]-1),d[0]);
      if (((bday.getMonth()+1)!=d[1])||(bday.getDate()!=d[0])||(bday.getFullYear()!=d[2])) { ErrCnt = 1; }
      if (ErrCnt < 0) {
        alert('Date is OK');  // for testing purposes only
        return true;
      } else { alert(ErrMsg[ErrCnt]); return false; }
    }
    </script>
    Format: dd/mm/yyyy OR dd-mm-yyyy<br>
    <input type="text" id="txt" value="16/08/1947">
    <button onclick="validDate()">Validate Date</button>
    Could also use the returned boolean values to branch to other actions.
    Last edited by jmrker; 01-13-2012 at 04:43 AM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Another alternative way to do the validation:

    Code:
    var a, b, c, d, e;
    a = "12/01/2012";
    b = new Date(a);
    c = y.getDate();
    c = (c<10)? '0'+c : c;
    d = y.getMonth() + 1;
    d = (d<10)? '0'+d : d;
    e = y.getFullYear();
    if (a !== c+'/'+d+'/'+e) alert('invalid date');
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Question

    Quote Originally Posted by felgall View Post
    Another alternative way to do the validation:

    Code:
    var a, b, c, d, e;
    a = "12/01/2012";
    b = new Date(a);
    c = y.getDate();
    c = (c<10)? '0'+c : c;
    d = y.getMonth() + 1;
    d = (d<10)? '0'+d : d;
    e = y.getFullYear();
    if (a !== c+'/'+d+'/'+e) alert('invalid date');
    I'm not sure that is correct for the question posed.
    OP wanted dd/mm/yyyy not mm/dd/yyyy format.
    See this ...
    Code:
    <script type="text/javascript">
    var d1 = new Date('12/01/2012');
    var d2 = new Date('01/12/2012');
    alert(d1.toString()+'\n'+d2.toString());
    </script>
    Also where does the 'y' come from in your code.
    Probably should be 'b' instead.

    Finally, I think the if statement compares your incorrect dd/mm/yyyy format which is mm/dd/yyyy with the new Date('mm/dd/yyyy') setting.

    to the formed format of dd/mm/yyyy

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Code:
    <script type = "text/javascript">
    
    function checkValidDate(yr,mmx,dd) {
    
    if (yr <1910 || yr >2011) {  // you may want to change 2011 to some other year!
    alert ("Year is out of range")
    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(2011,2,31)  // 31st February 2011

    In theory, there isn't any difference between the theory and practice. In practice, there is.

    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.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by jmrker View Post
    I'm not sure that is correct for the question posed.
    OP wanted dd/mm/yyyy not mm/dd/yyyy format.
    Data passed to Date as a string is in dd/mm/ccyy format for most of the world. It only defaults to mm/dd/ccyy if you configure your operating system to use that format - which you'd only do if you live in North America as that's the only place that uses that format.

    Presumably since the OP wants dd/mm format they don't live in North America and so the Date object on their computer will be expecting dd/mm - the same as it does for most people.

    Also where does the 'y' come from in your code.
    Probably should be 'b' instead.
    Yes, I renamed the field part way through writing the code and forgot to change all those references. So all those y references should be b.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Arrow

    Quote Originally Posted by felgall View Post
    Data passed to Date as a string is in dd/mm/ccyy format for most of the world. It only defaults to mm/dd/ccyy if you configure your operating system to use that format - which you'd only do if you live in North America as that's the only place that uses that format.

    Presumably since the OP wants dd/mm format they don't live in North America and so the Date object on their computer will be expecting dd/mm - the same as it does for most people.

    ...
    I guess I did not realize that as, yes I live in NA, and you seem to be in Australia. I'll bow to your knowledge of world-wide settings.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by jmrker View Post
    I guess I did not realize that as, yes I live in NA, and you seem to be in Australia. I'll bow to your knowledge of world-wide settings.
    I just did some further testing and it seems that JavaScript does not do quite what I expected in this instance. Even testing with a date where it is obvious from the values which parts of the date are which defining a new Date in my browser passing it '14/01/2012' it set the date to 1st February 2013 instead of the correct date. So it seems that I was wrong in assuming that Date.parse would do the logical thing and assume that whichever of the two values is over 12 is the day and that if they are both under 12 that it would examine the browser and operating system settings.

    So it looks like you are correct and the date would be assumed to be mm/dd/yyyy in all cases where the year doesn't come first - even where the values make it obvious that it is intended to be dd/mm/yyyy.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Smile

    Quote Originally Posted by felgall View Post
    I just did some further testing and it seems that JavaScript does not do quite what I expected in this instance. Even testing with a date where it is obvious from the values which parts of the date are which defining a new Date in my browser passing it '14/01/2012' it set the date to 1st February 2013 instead of the correct date. So it seems that I was wrong in assuming that Date.parse would do the logical thing and assume that whichever of the two values is over 12 is the day and that if they are both under 12 that it would examine the browser and operating system settings.

    So it looks like you are correct and the date would be assumed to be mm/dd/yyyy in all cases where the year doesn't come first - even where the values make it obvious that it is intended to be dd/mm/yyyy.
    Thanks for the further testing and advising me that I am not just another "(The) Ugly American"


  •  

    Posting Permissions

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