Java Reference
In-Depth Information
3 digits
4 digits
\\(? \\d{3}
\\)? [-. ]? \\d{3}
[-. ]? \\d{4}
Optional opening and
closing parentheses
Optional hyphen, period,
or space
Figure 4.5: A regular expression to validate a phone number
OK, regular expressions are rarely pleasing to the eye, so I've tried to
make it clearer by breaking it down as shown in Figure 4.5 .
The entire input must match the regular expression, so incomplete
phone numbers are also rejected. An example of entering an invalid
phone number is shown in Figure 4.6 , on the next page.
We've added a fairly sophisticated validation for the phone number with
a @Validate annotation and a regular expression mask. Think about how
much more code we'd need to implement this validation by parsing the
input string ourselves!
The Cancel Button
The last thing we need to do in the contact form is to turn all validations
off for the Cancel button. Otherwise, canceling the form won't work if
there are any invalid values that were entered by the user. We just need
to add the @DontValidate annotation to the cancel ( ) event handler:
Download email_06/src/stripesbook/action/ContactFormActionBean.java
@DontValidate
public Resolution cancel() {
getContext().getMessages().add(
new SimpleMessage("Action cancelled.")
);
return new RedirectResolution(ContactListActionBean. class );
}
 
 
 
Search WWH ::




Custom Search