Recipe8.2.Validating Using Regular Expressions


Recipe 8.2. Validating Using Regular Expressions

Problem

You want to validate data using a regular expression.

Solution

Use the mask validation type provided by the Validator:

<form name="ValidationTestForm">     <!-- Validate Social Security Number -->     <field property="ssn"             depends="required,mask">         <arg key="prompt.ssn"/>         <var>             <var-name>mask</var-name>             <var-value>^[0-9]{3}-[0-9]{2}-[0-9]{4}$</var-value>         </var>     </field> </form>

Discussion

A regular expression uses a general pattern notation that can be used to describe and parse text. Regular expressions have been around in one form or another since the 1960s. Using regular expressions, you can validate that a user's input matches a specific pattern. The pattern, simple or complex, is specified as a mask value that uses the regular expression pattern language. Without regular expressions, you would have to write a significant amount of custom code. Despite the power of regular expressions, many developers aren't comfortable using them. If you don't know regular expressions, learn them now because it could change your life. A good place to start is Mastering Regular Expressions by Jeffrey E. F. Friedl (O'Reilly).

The Validator supports the use of regular expressions through the mask bundled validator. A validation passes if the field value matches a given regular expression. You specify the regular expression through a Validator variable, defined in a var element. The name of the variable must be mask, and the value is the regular expression to use. If the field value matches the pattern, the validation passes; otherwise, the validation fails.

<var>     <var-name>mask</var-name>     <var-value>^[0-9]{3}-[0-9]{2}-[0-9]{4}$</var-value> </var>

If you are using Struts 1.1 or later, the regular expression must start with a caret (^) and end with the dollar sign ($). This restriction wasn't in place prior to Struts 1.1. Its inclusion, however, makes complete sense in the context of regular expressions. The caret notates the start of the string, and the dollar sign the end of the string. Supplying these values indicates you are validating the entire input string.


Regular expressions can look downright cryptic, but they are not that hard to understand once you get the basic notation. The expression shown in the Solution describes a pattern for validating an SSN. An SSN contains three digits, a hyphen, two digits, another hyphen, and four more digits. Valid values would be 123-45-6789, 000-00-0000, but not 123-84-Ab45 or 12-12-1234.

The best way to understand a regular expression is to put it into words. Here's how this would be done for the SSN expression. First, here's the expression:

^[0-9]{3}-[0-9]{2}-[0-9]{4}$

This expression would be interpreted as the following:

The beginning of the text (^), followed by three characters ({3}) each between 0 and 9 ([0-9]), followed by a hyphen (-), followed by two characters ({2}) each between 0 and 9 ([0-9]), followed by a hyphen (-), followed by four characters ({4}) each between 0 and 9 ([0-9]), followed by the end of the text ($).

The [0-9] is known as a character class. There are character classes for lowercase and uppercase alphabetic characters, numbers, and other character types. The {3} syntax is known as a quantifier. It specifies the quantity of the preceding character (or parenthesized character group). Additional quantifiers include * (0 or more), + (1 or more), and ? (0 or 1, i.e. optional).

Several shorthand notations exist for certain predefined character classes. For example, \d represents a digit and equates to using [0-9]. Knowing this, the SSN expression can be rewritten as:

^\d{3}-\d{2}-\d{4}$

When used by the Validator, a regular expression specified by the mask validator does replace the basic required validator. If you were to remove required from the Solution and no data was input, the form would pass validation though the mask validator was in place. At first glance this seems wrong; however, it works well in practice. If a field needs to be required, use required. Then, if the data needs to follow a certain text pattern, you can apply a regular expression validation using mask. This allows you to use mask on optional fields, and the mask only gets applied when the field has a non-empty value.

See Also

Regular expressions are powerful but important; it would be a disservice to cover the syntax and nuances of these expressions here. A better option is to consult a print or online reference.

Mastering Regular Expressions remains an essential tutorial and reference for regular expressions. This book has been recently updated to include, among other things, information relating to Java-based use of regular expressions. If you are new to regular expressions, this book provides a great start.

If you are familiar with regular expressions, Regular Expression Pocket Reference by Tony Stubblebine (O'Reilly) is a handy reference for the advanced programmer.

Steve Ramsay has put together a concise explanation of regular expressions at http://etext.lib.virginia.edu/helpsheets/regex.html.

The Validator uses the regular expression engine of the Jakarta ORO project (http://jakarta.apache.org/commons/oro). The Jakarta ORO JavaDocs provide an overview of the supported Perl 5-compliant regular expression syntax and can be found at http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/package-summary.html.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net