Date range validation using less code - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Date range validation using less code

Last post 03-04-2009 10:17 AM by Alec Pojidaev. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-31-2008 10:41 PM

    Date range validation using less code

    I've read more than one article about date range validation that relied on too much code for my taste, so I wrote simpler validation code that basically mimics how Excel calculates date ranges. This seems to be working without a hitch so far.

    Let's assume you have two date fields: start_date and end_date. You want to ensure that end_date is at least 10 days after start_date, and of course you want this validation to work on leap years as well. In order for this date range validation to work, you'll need to create a third, hidden field that will contain the difference in days between start_date and end_date; let's call this field diff.

    When start_date is entered, you convert this date string into a JavaScript date serial number, which is similar to an Excel date serial number but is the number of milliseconds since 1/1/1970. If the end date serial number is already defined, then calculate the difference in days and output it to the diff field. Write a similar OnAfterChange event handler for end_date. Once the diff field is populated, then you can use InfoPath's built-in data validation to ensure that the date range is at least ten days or whatever you want it to be.

    Here's the complete code:

    var nDateStart, nDateEnd;
    var oDiff = XDocument.DOM.selectSingleNode('my:myFields/my:diff');

    function msoxd_my_start_date::OnAfterChange(eventObj) {
      var str = eventObj.Site.text;
      if (str) {
        // Normalize date format (yyyy-mm-dd -> mm-dd-yyyy)
        nDateStart = Date.parse(str.replace(/(\d{4})-(.+)/,'$2-$1'));
        if (nDateEnd) {
          // Calculate difference (86400000 milliseconds = 1 day)
          oDiff.text = (nDate2-nDate1)/86400000;
        }
      }
    }

    function msoxd_my_end_date::OnAfterChange(eventObj) {
      var str = eventObj.Site.text;
      if (str) {
        // Normalize date format (yyyy-mm-dd -> mm-dd-yyyy)
        nDateEnd = Date.parse(str.replace(/(\d{4})-(.+)/,'$2-$1'));
        if (nDateStart) {
          // Calculate difference (86400000 milliseconds = 1 day)
          oDiff.text = (nDate2-nDate1)/86400000;
        }
      }
    }

    Cheers,
    Tom

    NOTE: If you want to count just business days, then read my other post, How to calculate business days. Even if you don't care about business days, the later post contains slightly improved code than what you see here.

  • 01-31-2008 10:51 PM In reply to

    Re: Date range validation using less code

    Thanks for sharing!

    Patrick Halstead
    Project Manager at Qdabra
  • 03-04-2009 10:17 AM In reply to

    Re: Date range validation using less code

    10basetom:
    I've read more than one article about date range validation that relied on too much code for my taste

     So you might find that codeless solution even more satisfying:

     http://alecpojidaev.wordpress.com/2008/12/30/infopath-codeless-programming-walkthrough-2/

     

Page 1 of 1 (3 items)
Copyright © 2003-2018 Qdabra Software. All rights reserved.
View our Terms of Use.