Auto-formatting a time field in InfoPath when it loses the focus

Use rules, conditions, the concat() function, and the substring() function in InfoPath to convert the way a time field is displayed from hhmm to hh:mm when the field loses the focus.

ADVERTISEMENTS

Problem

You want to facilitate the typing in of times in an InfoPath form by automatically converting a time entered into a time field from displaying as hhmm to displaying as hh:mm whenever the field loses the focus. For example, when a user types in 0730 as the time, it should change to 07:30 as soon as the user leaves the field.

Solution

Use rules, conditions, the concat() function, and the substring() function in InfoPath to automatically change the way a time entry is displayed after it has been updated.

Discussion

You can accomplish this functionality as follows:

  1. Create a New Blank Form in InfoPath.
  2. Add two text box controls to the form and rename them to timeField and helperField.
  3. Change the data type of the timeField field to Time (time) and change its format to 09:46.
  4. Add the following rule to the timeField field:
    Set a field's value: helperField = ""
    This rule will empty the helperField field to ensure that an OnChange event will be simulated on the helperField field when its value is set to something other than an empty string. Since you only want the formatting to take place when hhmm is entered, you must add the following condition to this rule:
    timeField matches pattern Custom pattern: 0000
    Note: You can enter this condition by selecting timeField from the first dropdown list box in the Condition dialog box, matches pattern from the second dropdown list box, and Select a pattern... from the third dropdown list box and type \d{4} into the Custom pattern field of the Data Entry Pattern dialog box.
  5. Add a second rule to the timeField field that says:
    Set a field's value: helperField = "calc"
    This rule will trigger the helperField field to convert the time in the timeField field. Since you only want the time to be converted to hh:mm when hhmm is entered, you must add the following condition to this rule:
    timeField matches pattern Custom pattern: 0000
    See note in previous step for instructions on how to set a condition that tests a pattern.
  6. Add a rule to the helperField field that says:
    Set a field's value: timeField = concat(substring(timeField, 1, 2), ":", substring(timeField, 3, 2), ":00")
    This rule will convert the time in the timeField field to the desired representation.
    NOTE: Use the Action and Insert Formula dialog boxes when creating this rule instead of just typing in the Value for the action for the rule.
  7. Delete the helperField field from the InfoPath form, but NOT from the data source. The rule you previously set on the helperField field will remain intact.

The time field should now automatically be converted from hhmm to hh:mm whenever it loses the focus and a valid time was entered.

 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services