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.
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:
- Create a New Blank Form in InfoPath.
- Add two text box controls to the form and rename them to timeField and helperField.
- Change the data type of the timeField field to Time (time) and change its format to 09:46.
- Add the following rule to the timeField field:
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:
Set a field's value: helperField = ""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.timeField matches pattern Custom pattern: 0000 - Add a second rule to the timeField field that says:
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:
Set a field's value: helperField = "calc"See note in previous step for instructions on how to set a condition that tests a pattern.timeField matches pattern Custom pattern: 0000 - Add a rule to the helperField field that says:
This rule will convert the time in the timeField field to the desired representation.
Set a field's value: timeField = concat(substring(timeField, 1, 2), ":", substring(timeField, 3, 2), ":00")
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. - 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.
Related InfoPath Articles:
