This step-by-step article describes how to create required (that is, "cannot be blank") string fields in your InfoPath form.
Introduction
If you create your own data source in InfoPath, you can designate a string field as a required field (that is, "cannot be blank") by modifying the field's properties in the Data Source task pane. However, if you instead use an external schema for your form's data source, you can designate a string field as a required field in one of two ways:
- You can create a simpleType element for the required field's type in the schema. This simpleType element contains a minLength restriction that is equal to or greater than 1.
-or- - If you cannot modify the schema, you can create a data validation rule in your InfoPath form.
The steps in this article describe each of these techniques that you can use to create a required string field on your form.
Designate a Required String Field in a Data Source That Is Built with InfoPath
InfoPath 2007
- In InfoPath, design a new blank form.
- In the Data source task pane, right-click the myFields group, and then click Add. The Add Field or Group dialog box appears.
- In the Name box, type ID. Click to select the Cannot be blank check box, and then click OK. Notice that the ID field appears in the Data source task pane with a red asterisk to indicate that it is a required field.
- Drag the ID field from the Data source task pane to your form.
- On the File menu, point to Preview, and then click Form.
- With the mouse pointer, pause on ID. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.
InfoPath 2003
- In InfoPath, design a new blank form.
- In the Data Source task pane, right-click the myFields group, and then click Add. The Add Field or Group dialog box appears.
- In the Field Name box, type ID. Under Validation, click to select the Cannot be blank check box, and then click OK. Notice that the ID field appears in the Data Source task pane with a red asterisk to indicate that it is a required field.
- Drag the ID field from the Data Source task pane to your form.
- On the File menu, point to Preview Form, and then click Default.
- With the mouse pointer, pause on ID. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.
Designate a Required String Field in Your External Schema
Create a custom schema file
In a text editor such as Notepad or in an XML editor, create the following schema. Then, save the schema as
Customschema.xsd.
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="myReqString" />
<xs:element name="LastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="myReqString">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Notice that the
FirstName field is typed with a
simpleType element that is named "myReqString," and "myReqString" has a restriction that the length must be at least one character.
Use the custom schema file
InfoPath 2007
- Start InfoPath.
- Design a new form that uses Customschema.xsd for the primary data source. To do this, follow these steps:
- On the File menu, click Design a Form Template.
- In the Design a Form Template task pane, click XML or Schema, and then click OK. The Data Source Wizard appears.
- Click Browse, select the Customschema.xsd file in the Open dialog box, click Open, and then click Next.
- Click No, and then click Finish to complete the wizard.
- Notice the fields in the Data source task pane. Notice that the FirstName field is marked with a red asterisk to indicate that the field cannot be blank. LastName is not marked with an asterisk. Therefore, it is not a required field.
- Drag the Employee group from the Data source task pane to your form, and then click Section with Controls.
- Test your form. To do this, follow these steps:
- On the File menu, point to Preview , and then click Form. The form opens in preview mode.
- Notice that only the First Name text box is displayed with a red asterisk. This red asterisk indicates that the field is required and cannot be blank.
- With the mouse pointer, pause on First Name. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.
InfoPath 2003
- Start InfoPath.
- Design a new form that uses Customschema.xsd for the primary data source. To do this, follow these steps:
- On the File menu, click Design a Form.
- In the Design a Form task pane, click New from Data Source. The Data Source Setup Wizard appears.
- Select XML Schema or XML data file for the data source type, and then click Next.
- Click Browse, select Customschema.xsd in the Open dialog box, and then click OK. Click Finish to complete the wizard.
- Notice the fields in the Data Source task pane. Notice that the FirstName field is marked with a red asterisk to indicate that the field cannot be blank. Because LastName is not marked with an asterisk, it is not a required field.
- Drag the Employee group from the Data Source task pane to your form, and then click Section with Controls.
- Test your form. To do this, follow these steps:
- On the File menu, point to Preview Form, and then click Default. The form opens in preview mode.
- Notice that only the First Name text box is underscored with a red line. This red line indicates that the field is required and cannot be blank.
- With the mouse pointer, pause on First Name. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.
Use an InfoPath Data Validation Rule to Create a Required String Field
Outlook 2007 or Outlook 2003
- Right-click the LastName field in the Data Source task pane, and then click Properties. The Field or Group Properties dialog box appears. Notice that the Cannot be blank option is disabled; this property is always disabled in the InfoPath user interface when your data source is an external data source. Click Cancel to dismiss the dialog box.
- Right-click the text box for the LastName field, and then click Text Box Properties.
- In the Text Box Properties dialog box, click Data Validation. The Data Validation dialog box appears.
- Click Add.
- Select is blank in the second condition list, type Last Name is a required field in the ScreenTip text box, and then click OK.
- Click OK to close the Data Validation dialog box, and then click OK to close the Text Box Properties dialog box.
- Test your form:
- Preview the form.
- Notice that the Last Name text boxis now displayed with a red asterisk or underscored with a red line. This red asterisk or red line indicates that the field is required and cannot be blank.
- With the mouse pointer, pause on Last Name. Notice the custom screen tip "Last Name is a required field." This is the custom screen tip that you typed for the data validation rule.