Validating using regular expressions

Regular expressions provide a powerful technique for validating fields. Phone numbers and Zip codes can be easily verified using regular expressions. The @Pattern annotation allows us to use a regular expression to verify the correct usage of these types of fields.

Getting ready

We will use the LicenseBean and LicenseBeanFacade classes from the ValidationApplication as discussed in the Validating persistent fields and properties recipe.

In order to illustrate the use of regular expressions we need to define allowable values for the restrictions field. This field may hold a combination of values reflecting driving constraints such as:

  • C Requires the use of corrective lenses
  • A Must be accompanied by an adult driver ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.