Child pages
  • How to validate field formats, such as a phone number
Skip to end of metadata
Go to start of metadata

Validating the format of String fields for patterns (such as a phone number) is easy with StringRegexValidator (named "regex" in the default validator configuration).

Simply add the validator the field in question, and supply a regular expression to match it against.


<validators>
    <field name="phone">
        <field-validator type="regex">
            <param name="expression">\([\d][\d][\d]\) [\d][\d][\d]-[\d][\d][\d][\d]</param>
            <message>Phone number must be in the format (XXX) XXX-XXXX</message>
        </field-validator>
    </field>
</validators>


If your expression tests against alpha characters, you may be interested in the "caseSensitive" parameter of with Validator as well. It defaults to "true".

  • No labels

2 Comments

  1. Parameters are:

    • fieldName
    • expression
    • caseSensitive

    So the attribute of the param label should be for this case "expression".

    <validators>
    <field name="phone">
    <field-validator type="regex">
    <param name="expression">(\d\d\d) \d\d\d-\d\d\d\d</param>
    <message>Phone number must be in the format (XXX) XXX-XXXX</message>
    </field-validator>
    </field>
    </validators>

    David A. Christensen

    1. Fixed - thanks for letting us know.