Skip to content
troyji edited this page Aug 16, 2011 · 15 revisions

Overview

@Pattern (or its alias @Matches) enforces the constraint that the value of the input element must match the specified regular expression.

Syntax

Syntax for @Pattern is:

@Pattern(regex=regular-expression, 
         [flags=regular-expression-flags] , 
         [label=label-text] , [message=message-text] , [groups=array-of-group-names])

Parameters

Name Type Required Description
regex Regular Expression yes A required parameter that specifies the regular expression to match against.
flags String no An optional parameter that specifies flags to the regular expression.
label String no An optional parameter that specifies the label for this input element. Labels are a way to provide friendly names to input elements. If the constraint fails to validate, the value in this field will be used in the default error message. If the constraint has a custom error message which contains {labe}, the value of this parameter will be substituted in the place of {label}.
message String no An optional parameter that specifies the error message to use when this constraint fails. The value for this field can contain interpolations of parameters used by the constraint . For example: The value is not a valid {label}.
groups Array no An array of group names that specifies the validation groups that this constraint belongs to.

Examples

Basic usage:

<input id = "socialSecurity"
       name = "socialSecurity"
       type = "text"
       data-constraints = '@Pattern(regex=/[0-9]{3}-[0-9]{2}-[0-9]{4}/)' />

Using flags:

<input id = "licensePlateNumber"
       name = "licensePlateNumber"
       type = "text"
       data-constraints = '@Pattern(regex=/[0-9]{3}-[A-Z]{3}/, flags="i")' />

Usage with optional parameters:

<input id = "socialSecurity"
       name = "socialSecurity"
       type = "text"
       data-constraints = '@Pattern(regex=/[0-9]{3}-[0-9]{2}-[0-9]{4}/, 
                                    label="social security number",
                                    message="That does not seem to be a valid {label}")' />
Clone this wiki locally