Show All

# Data validation events in the Events developer sample form

The Events developer sample form implements a variety of Microsoft Office InfoPath 2003 data validation events that can be used to validate data that is entered into a form. Data validation events can occur after a change has been made to a field or group, after a change has been made to a field or group but before the data is committed, and after a change has been made to a field or group and after the data is committed.

To use one of the data validation events, you must first create the event in the Field or Group Properties dialog box that is available from the Data Source task pane. To access this dialog box, right-click one of the fields or groups in the task pane, and then click Properties. On the Validation and Script tab, select the event you wish to create, and then click Edit; this opens Microsoft Script Editor (MSE) and displays the InfoPath-generated event handler.

Note Event handlers in InfoPath must be created in design mode.

The following table lists each of the data validation events implemented in the Events developer sample form, along with a description of how they are used.

Event | Description
--- | ---
OnValidate | Used to validate the data contained in the ContactDates group. When this event occurs, scripting code is used to verify that the dates used are within a specified range.
OnBeforeChange | Used to validate the data contained in the Email Campaign Start, Phone Contact Start, and Representative Visit date fields. When this event occurs, scripting code is used to implement business logic that checks for the existence of certain dates before others can be entered or removed.
OnAfterChange | Used to call a function that calculates the total campaign costs and updates the Campaign Cost field.

Using the OnValidate event

The OnValidate event occurs after a change has been made to a field. When a user makes a change to a field and then leaves the field, the OnValidate event can validate the data that the user entered.

In the Events developer sample form, the OnValidate event validates the data entered in the ContactDates group, which includes the Email Campaign Start, Phone Contact Start, and Representative Visit date fields. When a user enters a date in any of these fields, either by typing it or by using the Date Picker control, the OnValidate event occurs and the scripting code that it contains verifies that the dates entered do not exceed a specified interval. If they do exceed the specified interval, the ReportError method of the DataDOMEvent object, which is the eventObj argument passed to the event handler, is used to notify the user. The following example from the Event developer sample form shows the OnValidate event handler:

    function msoxd__ContactDates::OnValidate(eventObj)
    {
       var iNumberOfDays = 0;
       var objEmailDate = XDocument.DOM.selectSingleNode
          ('/Customers/CustomerInfo/ContactDates/EmailCampaignDt');
       var objPhoneContactDate = XDocument.DOM.selectSingleNode
          ('/Customers/CustomerInfo/ContactDates/PhoneContactDt');
       var objRepVisitDate = XDocument.DOM.selectSingleNode
          ('/Customers/CustomerInfo/ContactDates/RepVisitDt');

       // First validate the email and phone contact dates.
       if (!objEmailDate || !objPhoneContactDate)
          return;

       var emailDate = new Date(Application.FormatString
          (objEmailDate, "date", "dateFormat:MM-dd-yyyy"));
       var phoneContactDate = new Date(Application.FormatString
          (objPhoneContactDate, "date", "dateFormat:MM-dd-yyyy"));

       if (isNaN(emailDate) || isNaN(phoneContactDate))
          return;

       // Get the number of days between the two dates.
       iNumberOfDays = GetElapsedDays(emailDate, phoneContactDate);

       if (iNumberOfDays < REQUIRED_PHONE_EMAIL_INTERVAL)
       eventObj.ReportError(objPhoneContactDate,
          "The Phone Contact Start date must occur after " +
          REQUIRED_PHONE_EMAIL_INTERVAL +
          " days from the start of the Email Campaign.", false);

       // Now validate the phone contact and rep visit dates.
       if (!objRepVisitDate)
          return;

       var repVisitDate = new Date(Application.FormatString
          (objRepVisitDate, "date", "dateFormat:MM-dd-yyyy"));

       if (isNaN(repVisitDate))
          return;

       // Get the number of days between the two dates.
       iNumberOfDays = GetElapsedDays(phoneContactDate, repVisitDate);

       if (iNumberOfDays < REQUIRED_REPVISIT_PHONE_INTERVAL)
          eventObj.ReportError(objRepVisitDate,
             "The Representative Visit date must occur after " +
             REQUIRED_REPVISIT_PHONE_INTERVAL +
             " days from the start of the Phone Campaign.", false);

       // And finally, the email and rep dates.
       // Now check the Email Campaign start.
       iNumberOfDays = GetElapsedDays(emailDate, repVisitDate);

       if (iNumberOfDays > REQUIRED_REPVISIT_EMAIL_INTERVAL)
          eventObj.ReportError(objRepVisitDate,
             "The Representative Visit date cannot occur later than
             Email Campaign Start date plus " +
             REQUIRED_REPVISIT_EMAIL_INTERVAL + " days.", false);
    }

Using the OnBeforeChange event

The OnBeforeChange event occurs after a change has been made to a field but before the change has been committed. When a user makes a change to a field and then leaves the field, the OnBeforeChange event can validate the data the user entered and reject the change before it is committed.

In the Events developer sample form, the OnBeforeChange event validates the data entered in the Email Campaign Start, Phone Contact Start, and Representative Visit date fields. When a user enters a date in any of these fields, either by typing it or by using the Date Picker control, the OnBeforeChange event occurs and the scripting code that it contains verifies that the dates entered or removed are in the correct order. If they are not entered in the correct order, the ReturnMessage property of the DataDOMEvent object, which is the eventObj argument passed to the event handler, is used to set the text that will be displayed to the user, and the ReturnStatus property is used to indicate that the change will not be accepted. The following example from the Events developer sample form shows the OnBeforeChange event handler for the Email Campaign Start date field:

    function msoxd__EmailCampaignDt::OnBeforeChange(eventObj)
    {
       // Ensure that the Phone Contact Date is not filled in already.
       if (XDocument.DOM
          .selectSingleNode('/Customers/CustomerInfo/ContactDates/EmailCampaignDt')
          .text == "" && XDocument.DOM
          .selectSingleNode('/Customers/CustomerInfo/ContactDates/PhoneContactDt')
          .text != "")
       {
          eventObj.ReturnMessage = "Because you have entered a Phone Contact
             Start date, the Email Contact Start date information cannot be removed.";
          eventObj.ReturnStatus = false;
          return;
       }
    }

Using the OnAfterChange event

The OnAfterChange event occurs after a change has been made to a field and after the change has been committed. When a user makes a change to a field and then leaves the field, the OnAfterChange event can validate the data the user entered or call other functions that can be used to update other fields within the form.

In the Events developer sample form, the OnAfterChange event is used to call the CalculateTotalCampaignCost function. When a user enters a date in any of the date fields, either by typing it or by using the Date Picker control, and the data is committed, the OnAfterChange event occurs and the scripting code that it contains calls the CalculateTotalCampaignCost function, which calculates the total cost of the campaign and updates the Campaign Cost field. The following example from the Events developer sample form shows the OnAfterChange event handler for the Phone Contact Start date field:

    function msoxd__ContactDates::OnAfterChange(eventObj)
    {
       if (eventObj.IsUndoRedo)
       {
          // An undo or redo operation has occurred and the DOM is read-only.
          return;
       }
       CalculateTotalCampaignCost();
    }

Note The if statement used to check the IsUndoRedo property of the DataDOMEvent object, which is the eventObj argument passed to the event handler, is automatically generated by InfoPath when the OnAfterChange event handler is created. This statement can be used to handle a case in which the OnAfterChange event occurs as a result of an undo or a redo operation.

©2003-2004 Microsoft Corporation. All rights reserved. Permission to copy, display and distribute this document is available at: http://msdn.microsoft.com/library/en-us/odcXMLRef/html/odcXMLRefLegalNotice.asp