Skip to main content
Inspiring
April 30, 2008
Question

Javascript Date validation

  • April 30, 2008
  • 2 replies
  • 446 views
Hello,
I have form with two date fields. The default for the date 1 is the current date. Date 2 cannot be after date 1. Does anyone know of a javascript that would validate this on the client side. I don't want them to be allowed to hit submit if they don't the right date for date 2.

Thanks for your help.
    This topic has been closed for replies.

    2 replies

    April 30, 2008
    You can do this with ColdFusion using the dateDiff() function.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_c-d_28.html

    It'll be more reliable than JavaScript because it's on the server side. However, I'm sure there is a similar function in JS.

    Thanks,
    Mikey.
    Inspiring
    April 30, 2008
    From this page:

    http://www.webdeveloper.com/forum/archive/index.php/t-61259.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    " http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <meta name="Content-Style-Type" content="text/css">
    <title>Example</title>
    <script type="text/javascript">
    <!--
    function check (f) {
    var start = new Date (f.start.value);
    var end = new Date (f.end.value);
    start.setDate (start.getDate());
    if (end >= start) {
    alert ('Date 2 cannot be after Date 1');
    return false;
    }
    }
    // -->
    </script>
    <style type="text/css">
    <!--
    fieldset {padding:1ex; width:10em}
    label {display:block; margin:1em 0}
    input {display:block}
    button {display:block; margin:auto}
    -->
    </style>

    </head>
    <body>
    <form action="some-script.pl" onsubmit="return check(this)">
    <fieldset>
    <legend>Dates</legend>
    <label>Start Date<input name="start" onchange="this.value = new Date
    (this.value).toDateString()" type="text"></label>
    <label>End Date<input name="end" onchange="this.value = new Date
    (this.value).toDateString()" type="text"></label>
    <button type="submit">Submit</button>
    </fieldset>
    </form>
    </body>
    </html>


    --
    Ken Ford
    Adobe Community Expert - Dreamweaver
    Fordwebs, LLC
    http://www.fordwebs.com


    "jenn" <webforumsuser@macromedia.com> wrote in message
    news:fva0ov$eqj$1@forums.macromedia.com...
    > Hello,
    > I have form with two date fields. The default for the date 1 is the
    > current
    > date. Date 2 cannot be after date 1. Does anyone know of a javascript
    > that
    > would validate this on the client side. I don't want them to be allowed
    > to hit
    > submit if they don't the right date for date 2.
    >
    > Thanks for your help.
    >