Field Constraints

Text and number fields can be constrained to a certain format or range of values to ensure consistency in how a value is specified across your organization. For example, you might want to ensure that all phone numbers are input in the format 123-456-7890. Similarly, you might have a number field to store four-digit invoice numbers, and want to ensure that all values in that field fall between 1000 and 9999. Field data that has been input and formatted in a consistent manner makes searching for that data much easier.

Field data can only be restricted for certain field types: text fields and number fields. The restrictions that a particular text field will have are determined by regular expressions, a notation used to define a pattern of text. For more information, see Using Regular Expressions to Constrain Text Fields. The restrictions that a particular number field will have are determined by numeric relational operators. For more information, see Using Relational Operators to Constrain Number Fields. You can also view sample field constraints in Common Constraint Patterns. These constraints demonstrate the sample notation that can be used to constrain field data.

Using Regular Expressions to Constrain Text Fields

The following reference describes the parts of a regular expression that can be combined to form a text field constraint.

When specifying constraints, you may want certain characters to be automatically assigned to a field, which can be done by typing the characters where you want them to appear in the field. The exceptions are characters that have been reserved for use by regular expressions. A list of these characters can be viewed from the Symbol column of the table below. Reserved characters can be automatically assigned to a field by placing a backslash prior to the reserved character.

For example, if your organization decides that phone numbers should be specified as (310) 555-1212, you would specify the following expression:  \(\d\d\d\) \d\d\d-\d\d\d\d. Notice that the parentheses have been escaped with a backslash because parentheses are reserved for, while the dash has not been. Both parentheses and dashes are reserved characters. However, dashes can never be used without brackets, therefore they are treated as a regular character and do not require a backslash.

Assigning this constraint to a field will create visual indicators as to how field data should be formatted. In the example stated above, blank fields will look like "(   )    -    ." As you can tell, users will not need to type the parentheses or the dash when specifying a phone number. These symbols are automatically shown to indicate what the format of a phone number should look like.  

The following table describes each regular expression that can be used to establish a pattern that field data must match.

Regular Expression Syntax Elements
Name Symbol Description
Any Character . Any single character.
Character in Range [] Any character inside the brackets. For example, the expression [abc123] allows only any of the following characters:  "a," "b," "c," "1," "2," or "3."
Character Not in Range [^] Any single character except for those inside the brackets. For example, the expression [^abc123] allows only any character except for:   "a," "b," "c," "1," "2," or "3."
Range Character [-] Any single character contained within the specified range. For example, the expression [0-9] allows only any number that falls between 0 and 9.
Beginning of Input ^ Requires the expression that follows it at the beginning of the user-defined value. For example, the expression ^[abc123] allows only field data that starts with either "a," "b," "c," "1," "2," or "3."
End of Input $ Requires the expression that precedes it at the end of the user-defined value. For example, the expression [abc123]$ allows only field data that ends with either "a," "b," "c," "1," "2," or "3."
Not ! Requires that the expression following the symbol (!) not be found in the field data. For example, the expression a!b allows only field data containing an "a" when it is not immediately followed by "b."
Or | Requires one of two expressions. For example, the expression he|she allows only field data that is set to either "he" or "she."
0 or More * The preceding expression can occur zero or more times. For example, the expression [0-9]* allows any set of consecutive digits or no digits at all.
1 or More + The preceding expression can occur one or more times. For example, the expression [0-9]+ allows any set of consecutive digits.
Previous Statement is Optional ? The preceding expression is optional. Data satisfying the expression may be specified as field data or a user can choose to not enter it. For example, the expression [0-9][0-9]? allows only a single or double digit.
Group () Groups an expression together. For example, the expression (t|T)he allows only field data that is set to either "the" or "The".
Escape Character \ Either an abbreviation (see table below) or that the next character be translated literally. This character should only be used for reserved characters, such as those listed under the Symbol column of this table. For example, \d+ allows only one or more digits, while \d\+ allows a digit followed by a plus sign.

Character Classes

A character class can be used to restrict the character for a particular field.

Character Class Syntax Elements
Name Symbol Description
Alphanumeric [[:alnum:]] Any alphanumeric character.
Alphabetic [[:alpha:]] Any alphabetical character in the following ranges:  a-z and A-Z.
Space/Tab [[:blank:]] A space or a tab.
Digit [[:digit:]] Any digit. A valid character is a whole number from 0 to 9.
Lower-case [[:lower:]] Any lower-case character (i.e., a-z).
Printable [[:print:]] Any printable character.
Punctuation [[:punct:]] Any punctuation character.
Space [[:space:]] Any whitespace character.
Upper-case [[:upper:]] Any upper-case character (i.e., A-Z).
Hexadecimal [[:xdigit:]] Any hexadecimal digit (i.e., 0-9, a-f and A-F).
Word [[:word:]] Any word character. Valid characters are all alphanumeric characters and underscore.

Abbreviations

The following table describes the various abbreviations for specifying a regular expression.

Regular Expression Abbreviation Syntax Elements
Name Symbol Description
Character . Any single character.
Decimal Digit \d Any single decimal digit. Corresponding syntax: [[:digit:]]
Non-Decimal Digit \D Any character except for a single decimal digit. Corresponding syntax: [^[:digit:]]
Space \s A single space character. Corresponding syntax: [[:space:]]
Non-Space \S Any character except for a single space character. Corresponding syntax: [^[:space:]]

Using Relational Operators to Constrain Number Fields

The following reference describes the relational operators that can be combined to form a numeric field constraint.

The following table describes each operator that will be used to compare a value in a field with the range of valid values for that field. These must be used in conjunction with number values: for instance, to specify that the value must be greater than 1000, the constraint would be written as ">1000".

Relational Operator Syntax Elements
Name Symbol Description
Less Than < A valid value is less than the specified number.
Greater Than > A valid value is greater than the specified number.
Less Than or Equal To <= A valid value is less than or equal to the specified number.
Greater Than or Equal To >= A valid value is greater than or equal to the specified number.
Equal To = A valid value is equal to the specified number.
Not Equal To <> A valid value does not equal the specified number.

Boolean Logic Operators

The following table describes the Boolean operators that can be used to combine relational operators. Either the name or the symbol can be used; names are not case sensitive.  For example, to specify that a value must be between 1000 and 9999, the constraint could be written either as ">=1000 AND <=9999" or as ">=1000 & <=9999".

Boolean Logic Syntax Elements
Name Symbol Description
AND & Both constraints must be met for the value to be valid.
OR | At least one constraint must be met for the value to be valid.
NOT ! The constraint must not be met for the value to be valid.

Order of Precedence

A numeric constraint can be created with more than one Boolean operator. In that case, the string will be analyzed in the following order: NOT will be applied first, followed by AND, followed by OR. Parentheses will be used for grouping.

Examples

The following examples demonstrate the use of the various relational and Boolean operators.

Order of Operation Syntax Elements
Desired Constraint Numeric Constraint String
A valid value must be greater than four. >4
A valid value must not be greater than 999. !>999
A valid value must be between 1 and 10, not including 1 and 10. 1 < & < 10
A valid value must be either between 100 and 200 or between 500 and 900, including 100, 200, 500 and 900. (>=100 & <=200) | (>=500 & <=900)

Common Constraint Patterns

This is a short list of common patterns that can be used to restrict field data, provided only as an introduction to the many uses of regular expressions to ensure proper data formatting. These, like any other patterns, can be modified to best suit the needs of your organization.

Note:  These common patterns primarily use abbreviations for simplicity. However, you may also use any supported symbols when setting up a constraint.

Common Text Constraint Patterns

Text Constraint Examples
Type Pattern Example
Phone Number

((xxx) xxx-xxxx format)

\(\d\d\d\) \d\d\d-\d\d\d\d (562) 988-1688
Phone Number

(xxx-xxx-xxxx format)

\d\d\d-\d\d\d-\d\d\d\d 562-988-1688
Social Security Number

(xxx-xx-xxxx format)

\d\d\d-\d\d-\d\d\d\d 123-45-6789
Zip Code

(xxxxx or xxxxx-xxxx format)

\d\d\d\d\d(-\d\d\d\d)? 90807

Or:

90807-1234

Common Numeric Constraint Patterns

Number Constraint Examples
Type Pattern
Four-digit number >=1000 &  <=9999
Positive numbers only >=0

Constraint Violation Messages

A message will appear when a user attempts to assign a field value that does not satisfy the constraint configured for a field. This message can be customized, so that your organization may provide specific instructions on how a field value should be formatted.

In addition to informing users of the proper format, you may want to consider providing examples of how field data should be formatted. For example, if your regular expression looked like:  \(\d\d\d\) \d\d\d-\d\d\d\d, you may want to use the following message:

The value assigned to the field has not been properly formatted. The proper format for this field is the following: (xxx) xxx-xxxx. An example of a properly formatted phone number is: (562) 988-1688.