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
The entire input must match the regular expression, so incomplete
phone numbers are also rejected. An example of entering an invalid
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:
@DontValidate
public
Resolution cancel() {
getContext().getMessages().add(
new
SimpleMessage("Action cancelled.")
);
return new
RedirectResolution(ContactListActionBean.
class
);
}
Search WWH ::
Custom Search