Postal codes (also known as post codes or ZIP codes) are a series of characters
appended to a postal address for the purpose of sorting mail. Most countries
use 4, 5, 6, or 9 digit numeric strings, while others use both numbers
and letters. In your form, you may want to accept a wide range of valid
postal code formats while filtering invalid ones. You can do this by defining
the field’s presentation format and providing input constraints.
Input Constraints
In particular, you must specify the patterns that the postal code fields
may accept. For example, to specify a Canadian postal code (two sets of
three alternating alphanumeric characters, such as V9A 1G2) you would create
a pattern that allows:
- a letter
- a number
- a letter
- an optional space
- a number
- a letter
- a number
You must use a Unix-style regular expression to specify this pattern. For
example, the following regular expression creates the pattern described
above:
([A-Za-z]{1}[0-9]{1}[A-Za-z]{1})\s?([0-9]{1}[A-Za-z]{1}[0-9]{1})
A field formatted with only this expression would accept user input formatted
in only two ways:
To add additional postal code styles, you need to add additional acceptable
patterns. For example, to add 4, 5, or 6 character digit strings, or a
9 digit string that may contain a dash, you would add the following patterns:
(\d{4})
(\d{5})
(\d{6})
(\d{5})-?(\d{4})
Presentation
For every pattern you create as an input constraint, you must have a corresponding
presentation patternref. The patternref allows you to specify how the user’s
input is displayed in the form. If there are multiple patterns, the first
patternref corresponds to the first pattern, the second patternref corresponds
to the second pattern, and so on. You must use a Unix-style regular expression
to specify this patternref. The follow example shows the patternrefs for
the 6 character alphanumeric code, the 4, 5, and 6 digit codes, and the
9 digit code that contains a dash:
$1 $2
$1
$1
$1
$1-$2
Note that the patternref for the 4, 5, and 6 digit codes is exactly the
same (indicating a single string). However, each must be listed separately
to ensure they correspond with the correct input constraint pattern.
Examples
The following code sample shows how to accept multiple postal code formats
and allow them to display in the format chosen by the user. This example
also uses casetype to ensure that any letters are displayed as upper case:
<field sid="FIELD4">
<label>Postal/ZIP code
</label>
<format>
<datatype>string
</datatype>
<constraints>
<patterns>
<pattern>([A-Za-z]{1}[0-9]{1}[A-Za-z]{1})\s?([0-9]{1}[A-Za-z]
{1}[0-9]{1})
</pattern>
<pattern>(\d{4})
</pattern>
<pattern>(\d{5})
</pattern>
<pattern>(\d{6})
</pattern>
<pattern>(\d{5})-?(\d{4})
</pattern>
</patterns>
</constraints>
<presentation>
<patternrefs>
<patternref>$1
$2
</patternref>
<patternref>$1
</patternref>
<patternref>$1
</patternref>
<patternref>$1
</patternref>
<patternref>$1-$2
</patternref>
</patternrefs>
<casetype>upper
</casetype>
</presentation>
</format>
<value>
</value>
</field>
Exceptions to this practice
There are no exceptions to this practice.