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 14 of 14
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Validating specific date format

    Does anyone have any validation code that validates date according to dd/mmm/yyyy or dd-mmm-yyyy format.

    I tried searching on google and also yahoo but all the codes basically validates date based on dd/mm/yy or mm/dd/yy.

    If there is any useful resource or sample code, please post it.

    Thank you very much in advance

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    This doesn't match your date format, but it explains pretty well how you build it, and you should be able to reverse the order easily: RegExp - validate date.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    I think a1jit wants to validate dates in the form
    23-MAR-2004 or 23/AUG/2004

    I am sure that this can be done, but why does he want to have his dates entered in this format to start with? An explanation of exactly what it is we are trying to achieve would help.

    Another "Thank you in advance" first-time-visitor! Yuk! Puke!

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh lol I thought he just wanted year as 4 digits and simply put that 3rd m by mistake

    the 6th edit field in the 2nd form
    http://www.the-hive.dk/~donp/?f=6#validator
    Last edited by Garadon; 05-03-2004 at 09:43 AM.

  • #5
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah! ur right . i want the date to be validated in this format (23-MAR-2004 or 23/AUG/2004)

    Actually im doing this for the users. They want the date to be entered in this format. So, i need to validate before i can actually store it in the database.

    Help pleASE!!!

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    This code may not look elegant because it does not use regex. But the advantage of it over regex is it validates not only the format but also the validity of the date.
    Code:
    <script type="tex/javascript">
      var arr = [];
      arr['JAN'] = 0;
      arr['FEB'] = 1;
      arr['MAR'] = 2;
      arr['APR'] = 3;
      arr['MAY'] = 4;
      arr['JUN'] = 5;
      arr['JUL'] = 6;
      arr['AUG'] = 7;
      arr['SEP'] = 8;
      arr['OCT'] = 9;
      arr['NOV'] = 10;
      arr['DEC'] = 11;
    
    function isValidDate(strDate){
      var arrDate = strDate.split('-');
      if (arrDate.length==3 && strDate.length==11){
        var d = new Date(arrDate[2], arr[arrDate[1]], arrDate[0]);
        if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1]] && d.getDate()==arrDate[0]){
           return true;
        }
      }
      return false;
    }
    alert(isValidDate('31-JAN-2004')); //true
    alert(isValidDate('29-FEB-2004')); //true
    alert(isValidDate('30-FEB-2004')); //false
    alert(isValidDate('31-APR-2004')); //false
    </script>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    This allows only for a hyphen as the delimiter.
    So to allow for / as well:-

    function isValidDate(strDate){
    strDate.replace (/\//g, "-"); // replaces / by -
    var arrDate = strDate.split('-');

    and in case they enter spaces as well:-

    strDate.replace (/\s/g,""); // deletes all spaces

    My experience is that if the user can conceivably and by whatever improbable contortion fill in a form incorrectly, they will!

  • #8
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thankx a lot for the help. I have a problem executiing the code. i have added a text field in your code. But i dont know why its not checking for the date validity. Can you pleaseeee show me how to link the javascript with a text field so that when user click the submit button, it checks for the date validity..thank you very very much!

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Is this what you mean? That is, how to call the function when the submit button is clicked.

    <FORM action="http://yourdomain.com/cgi-bin/formmail.pl"
    method="POST" name="TheNameOfTheForm" onSubmit="return isValidDate(this.Submitbutton)">

    <input type="submit" name="Submitbutton" value="Submit This Form">

    </FORM>

    But as it stands the function isValidDate() rejects inavlid entries but does not offer the opportunity for the user to re-enter the date correctly. You need to add something like

    fieldname = ""; //erase the field
    fieldname.focus(); // and reset the focus
    return false;

  • #10
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, im trying to modify the code pasted by glenngv. His code basically validates date based on the default date value. I want his code to validate date entered in a text field, not using default value. it means that i want his code to function this way

    Basically user has a text field and also a submit button. so, user key in a date and clicks the submit button. After the submit button is clicked, the date is validated based on the javascript code pasted by glenngv.

    If the date in not a valid date(means that it doesnt match the format[dd-mmm-yyyy]), then it says invalid date. If the date is a valid date, it submits the page.

  • #11
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I Managed to get the code running. This is how the code looks like

    -------------------------------------------------------------------------
    <html>
    <head>
    <title>Untitled Document</title>


    <script type="text/javascript">


    function isValidDate(strDate){

    var str=document.getElementById(strDate).value;

    var arr = [];
    arr['JAN'] = 0;
    arr['FEB'] = 1;
    arr['MAR'] = 2;
    arr['APR'] = 3;
    arr['MAY'] = 4;
    arr['JUN'] = 5;
    arr['JUL'] = 6;
    arr['AUG'] = 7;
    arr['SEP'] = 8;
    arr['OCT'] = 9;
    arr['NOV'] = 10;
    arr['DEC'] = 11;
    var arrDate = str.split('-');
    if (arrDate.length==3 && str.length==11){
    var d = new Date(arrDate[2], arr[arrDate[1]], arrDate[0]);
    if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1]] && d.getDate()==arrDate[0]){
    alert("its valid");
    return true;
    }
    }
    alert("Not a valid date");
    return false;
    }

    </script>

    </head>

    <body>
    <form id="myForm">

    <input type="text" id="fDate"><input type="button" onclick="isValidDate('fDate')" value="Submit">

    </form>
    </body>
    </html>


    -------------------------------------------------------------------------

    The problem here is that, lets say the user enters 1-Jan-1990, it says the date is invalid. If user enters 01-Jan-1990, it says the date is valid. How do i make 1-Jan-1990 as valid? Both the dates suppose to be valid, but just because 1-Jan-1990 has only 1 digit for the day, it becomes invalid. How do i solve this problem

  • #12
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    gUYS,

    THANKX A lotttttt for all the help..manage to solve the problem. i replaced the if statement with this one

    if (arrDate.length==3 && str.length==11 || str.length==10)

    THANKX again ...

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The code I posted is generic. You can validate multiple date fields. With what you've done on it, you can only validate specific fields.

    This is how I'd implement it:
    Code:
    <html>
    <head>
    <script type="text/javascript">
      var arr = [];
      arr['JAN'] = 0;
      arr['FEB'] = 1;
      arr['MAR'] = 2;
      arr['APR'] = 3;
      arr['MAY'] = 4;
      arr['JUN'] = 5;
      arr['JUL'] = 6;
      arr['AUG'] = 7;
      arr['SEP'] = 8;
      arr['OCT'] = 9;
      arr['NOV'] = 10;
      arr['DEC'] = 11;
    
    //validates date formats: dd-mmm-yyyy, d-mmm-yyyy
    function isValidDate(strDate){
      var arrDate = strDate.split('-');
      if (arrDate.length==3 && (strDate.length==11 || strDate.length==10)){
        var d = new Date(arrDate[2], arr[arrDate[1].toUpperCase()], arrDate[0]);
        if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1].toUpperCase()] && d.getDate()==arrDate[0]){
           return true;
        }
      }
      return false;
    }
    
    function validateFields(f){
      if (!isValidDate(f.fDate.value)) {
         alert("Invalid date.");
         f.fDate.focus();
         return false;
      }  
      return true;
    }
    </script>
    
    </head>
    <body>
    <form id="myForm" name="myForm" action="javascript:alert('Submitted.')" onsubmit="return validateFields(this)">
    <input type="text" id="fDate" name="fDate" />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    I modified it in such a way that the month part is case-insensitive.
    Last edited by glenngv; 05-04-2004 at 05:28 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #14
    New Coder
    Join Date
    May 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very very very much bro..

    i really really appreciate it!!

    thankxxxxx man!!!!!


  •  

    Posting Permissions

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