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

Thread: Date Validation

  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Validation

    I am wanting to validate a date field where the user must enter a date that is either equal to the current date or greather than the current date. It must not be prior to the current date. The form is coded in ASP and I have a current date field that is populated automatically using the following code:
    value="<%= Date() %>". Can I insert that value into my JavaScript as a variable and then simply compare it with the value entered into the other date field.

    Any help would be greatly appreciated!

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Location
    UK
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sure

    Yes, if its in a text box in a form.

    You would simply set a variable to that form elements value.

    Code:
    <script type="text/javascript">
    
    var oldDate = document.form.olddate.value;
    var nowDate = document.form.newdate.value;
    if (oldDate < nowDate) {
        alert("date is before todays date");
    } else {
        alert("date is more than, or equal");}
    </script>
    Last edited by bfsog; 03-09-2005 at 10:38 PM.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function validate(oForm){
      //validate date
      var defaultDate = new Date(oForm.date.defaultValue); //initial value
      var newDate = new Date(oForm.date.value); //new value
      if (isNaN(newDate)){
        alert("Invalid date.");
        oForm.date.focus();
        return false;
      }
      if ((newDate.getMilliseconds() - defaultDate.getMilliseconds()) < 0){
        alert("Date must not be earlier than the current date.");
        oForm.date.focus();
        return false;
      }
      //other validations here...
      return true;
    }
    </script>
    </head>
    <body>
    <form name="form1" action="page2.asp" method="post" onsubmit="return validate(this)">
    ...
    <input type="text" name="date" value="<%= Date() %>" />
    ...
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    Last edited by glenngv; 03-10-2005 at 03:42 AM.
    Glenn
    ____________________________________

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


  •  

    Posting Permissions

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