The Java EE 5 Tutorial

Registering a Validator on a Text Field

By nesting the validateLongRange tag within a text field’s component’s tag, the page author registers a LongRangeValidator onto the text field. This validator checks whether the component’s local data is within a certain range, defined by the validateLongRange tag’s minimum and maximum attributes.

In the case of the greeting page, you need to validate the number the user enters into the text field. So, you add a validateLongRange tag inside the inputText tag. The maximum and minimum attributes of the validateLongRange tag get their values from the minimum and maximum properties of UserNumberBean using the value expressions #{UserNumberBean.minimum} and #{UserNumberBean.maximum}. See Backing Beans for details on value expressions.

After adding the validateLongRange tag, the page looks like this:

<%@ taglib uri="" prefix="h" %>
<%@ taglib uri="" prefix="f" %>
    <h:form id="helloForm1">
        <h2>Hi. My name is Duke. I’m thinking of a number from
         <h:outputText lang="en_US"
             value="#{UserNumberBean.minimum}"/> to
         <h:outputText value="#{UserNumberBean.maximum}"/>.
        Can you guess it?</h2>
        <h:graphicImage id="waveImg" url="/" />
        <h:inputText id="userNo" label="User Number"
                     maximum="#{UserNumberBean.maximum}" />

For more information on the standard validators included with JavaServer Faces technology, see Using the Standard Validators.