Java Reference
In-Depth Information
41 <h:inputText id="heightInputText" required="true"
42 requiredMessage="Height is required"
43 validatorMessage="Height must be between 3.5 and 9.5"
44 value="#{validateForm.heightString}">
45 <f:validateDoubleRange minimum="3.5" maximum="9.5"/>
46 </h:inputText>
47 <h:message for="heightInputText" style="color:red"/>
48 </h:panelGrid>
49
50 <h:commandButton value="Submit" />
51
52 <h:outputText style="color:red"
53 value="#{validateForm.response}" />
54 </h:form>
55 </h:body>
56 </html>
validate double range
For each input text field, set its
required
attribute
true
(lines 14, 23, 32, 41) to indi-
cate that an input value is required for the field. When a required input field is empty, the
requiredMessage
is displayed (lines 15, 24, 33, 42).
The
validatorMessage
attribute specifies a message to be displayed if the input field is
invalid (line 16). The
f:validateLength
tag specifies the minimum or maximum length of
the input (line 18). JSF will determine whether the input length is valid.
The
h:message
element displays the
validatorMessage
if the input is invalid. The ele-
ment's
for
attribute specifies the
id
of the element for which the message will be displayed
(line 20).
The
f:validateRegex
tag specifies a regular expression for validating the input (lineĀ 27).
For information on regular expression, see Appendix H.
The
f:validateLongRange
tag specifies a range for an integer input using the
minimum
and
maximum
attribute (line 36). In this project, a valid age value is between
16
and
120
.
The
f:validateDoubleRange
tag specifies a range for a double input using the
minimum
and
maximum
attribute (line 45). In this project, a valid height value is between
3.5
and
9.5
.
required
attribute
requiredMessage
validatorMessage
f:validateLength
h:message
f:validateRegex
f:validateLongRange
f:validateDoubleRange
L
ISTING
33.12
ValidateFormJSFBean.java
1
package
jsf2demo;
2
3
import
javax.enterprise.context.RequestScoped;
4
import
javax.inject.Named;
5
6 @Named(value =
"validateForm"
)
7 @RequestScoped
8
public class
ValidateFormJSFBean {
9
private
String name;
10
private
String ssn;
11
private
String ageString;
12
private
String heightString;
13
14
public
String getName() {
15
return
name;
16 }
17
18
public void
setName(String name) {
19
this
.name = name;
20 }
21
22
public
String getSsn() {
23
return
ssn;
Search WWH ::
Custom Search