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 2 of 2

Thread: Date Validation

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

    Date Validation

    Hi,

    Please help to validate the sart date and end date.

    If start date is greater than end date it should throw the error message as a pop up, please help me how to achive tis?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    <html>
    <head>
    </head>
    
    
    <body>
    
    Enter the starting date (DD/MM/YYYY) <input type = "text" id = "start"><br>
    Enter the ending date (DD/MM/YYYY) <input type = "text" id = "end"><br>
    <input type = "button" value = "Check Dates" onclick = "validate()">
    
    <script type = "text/javascript">
    
    function validate() {
    
    var s = document.getElementById("start").value;
    s = s.replace(/[\s\-\:]/g, "/");
    var e = document.getElementById("end").value;
    e = e.replace(/[\s\-\:]/g, "/")
    var ss = s.split("/");
    var es = e.split("/");
    var sv = checkValidDate(ss[2],ss[1],ss[0]);
    if (sv) {
    var ev = checkValidDate(es[2],es[1],es[0]);
    }
    if (sv && ev) {  // both dates valid
    var nd1 = new Date(ss[2], ss[1]-1, ss[0])
    var nd2 = new Date(es[2], es[1]-1, es[0])
    if (nd1 > nd2) {  // check start not before end - change to >= if required
    alert ("Invalid data - start date is after the end date!\nRe-enter the dates, please.");
    document.getElementById("start").value = "";
    document.getElementById("end").value = "";
    return false;
    }
    }
    
    }
    
    function checkValidDate(yr,mmx,dd) {
    
    if (yr <1910 || yr >2013) {  // you may want to change 2013 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!\nRe-enter the dates, please!");
    document.getElementById("start").value = "";
    document.getElementById("end").value = "";
    return false; 
    }
    else {
    alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
    return true;
    }
    
    }
    
    </script>
    
    </body>
    </html>

    Lottery: A tax on people who are bad at math.
    Last edited by Philip M; 08-09-2012 at 03:35 PM. Reason: Noticed typo

    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
    •