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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Location
    Cincinnati
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validating date in seperate fields on form?

    I see 10,000 ways to validate date fields on forms. But all of them are based on a single text box using mm/dd/yyyy type formats. My forms have the date seperated into individual fields. a 2 digit month in one input box, 2 digit day input box and 4 digit year input box.

    month.text
    day.text
    year.text

    I'm using asp.net forms so using range validator I validate that the month field is between 1-12 day is between 1-31 and year is between 1800-2250, but that really doesnt do much good. Users can still enter invalid dates such as: 02/30/2010.

    I also have a seperated date field that must be either "today" or a future date.

    There are scripts everywhere for these if we used the old standard single text box, but since mine are seperate fields how do I do this?
    Last edited by TronixGT; 05-24-2009 at 03:27 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Read in the values of the three fields into this script:-


    Code:
    <script type = "text/javascript">
    
    function checkValidDate() {
    var yr = 2009;
    var mm = 1;   //  mm = 0 - 11 so 1 is February
    var dd = 31
    var nd = new Date();
    nd.setFullYear(yr,mm,dd);  // YYYY,MM(0-11),DD
    var ndmm = nd.getMonth();
    if (ndmm != mm) {
    alert (dd + "/" + mm + "/" + yr  + " is an Invalid Date!");
    return false; 
    }
    else {
    alert (dd + "/" + mm + "/" + yr  + " is a Valid Date");
    }
    }
    
    checkValidDate()
    </script>
    Likewise:-

    Code:
    <script type = "text/javascript">
    
    function checkFutureDate() {
    var end_year = 2009;
    var end_month = 4;  // 0 - 11 so 4 is May
    var end_day = 2;
    
    var now = new Date().getTime();
    var d = new Date();
    d.setFullYear(end_year, end_month, end_day);  // YYYY,MM(0-11),DD
    var selectedDate = d.getTime();
    if (selectedDate <= now) {  // after today's date
    // if (selectedDate < now) {  // including today's date or after
    alert ("Date must be after today's date!");
    return false;
    }
    alert ("Date is valid");
    return true;
    }
    
    checkFutureDate()
    </script>


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-24-2009 at 04:48 PM.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Location
    Cincinnati
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Philip, That at least got me started in the right direction.

    The first script basically accomplished the same thing that RangeValidation in asp.net does. it made sure month is 1-12 day is 1-31.. etc. But it did not account for days like february 29th or february 30th. I have found some code that does work, and does check for a true valid date. That seems to be working now.

    I am trying to use the FutureDate function you posted, but it is allowing any date in the month of may and it should only allow today and future dates.. so it doesnt quite check the month/day right. Is this a MM/DD/YYYY type format issue? I can't figure this out.

    Here is what I have..

    Thanks in advance!

    Code:
    <HTML>
      <HEAD>
        <SCRIPT LANGUAGE="JavaScript">
       function isDate(year, month, day) {
       month = month - 1; // javascript month range 0 - 11
       var tempDate = new Date(year,month,day);
       if ( (year == tempDate.getFullYear()) &&
        (month == tempDate.getMonth()) &&
        (day == tempDate.getDate()) ) {
          return true;
       } else {
          return false;
       }
    }
          function checkValidDate(form) {
            with (form) {
              var monthValue = document.getElementById('month').value;
              var dayValue = document.getElementById('day').value;
              var yearValue = document.getElementById('year').value;
              if (!isDate(yearValue,monthValue,dayValue)) {
                alert("This is not a valid date!");
                return false;
              }
            }
            alert ("Date is valid date");
            return true;
          }
         function checkFutureDate() {
             var end_month = document.getElementById('month').value;  // 0 - 11 so 4 is May
             var end_day = document.getElementById('day').value;
             var end_year = document.getElementById('year').value;
    
             var now = new Date().getTime();
            var d = new Date();
            d.setFullYear(end_year, end_month, end_day);  // YYYY,MM(0-11),DD
            var selectedDate = d.getTime();
             if (selectedDate <= now) {  // after today's date
               // if (selectedDate < now) {  // including today's date or after
                alert ("Date must be today or after today's date!");
               return false;
          }
              checkValidDate(this);
              return true;
    }
    </script>
    
    
      </HEAD>
      <BODY>
        <FORM ONSUBMIT="checkFutureDate()">
    
          <INPUT TYPE="text" NAME="month" SIZE="2" MAXLENGTH="2">
          <INPUT TYPE="text" NAME="day" SIZE="2" MAXLENGTH="2">
          <INPUT TYPE="text" NAME="year" SIZE="4" MAXLENGTH="4">
          <INPUT TYPE="submit" VALUE="Validate">
        </FORM>
      </BODY>
    </HTML>
    Last edited by TronixGT; 05-24-2009 at 10:27 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    The scripts I offered you work fine for me.

    checkValidDate(this);
    return true;

    will always return true. Delete the line in blue.

    The reason it pases any date in May is that you have not taken on board that months in Javascript are 0-11, so May is month 4. Month 5 is June, and any date in June is ahead of today.

    There is nothing in your scripts to validate that the values entered are numbers.

    <SCRIPT LANGUAGE="JavaScript"> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
    Last edited by Philip M; 05-25-2009 at 08:24 AM.


  •  

    Tags for this Thread

    Posting Permissions

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