java.net member

Rechercher dans ce site

JSF Validation- Part 1

>> 11 May 2010




Validation is used to verify that a component get an expected value.

JSF has several standard validators, ready to use. You can also create your own custom validators

Note:
For simplicity interactions with beans are omitted

Some standard validators with examples :

DoubleRangeValidator,
Numeric validation (double or float), value must be between minimum and maximum values

LongRangeValidator,
Numeric validation (long or int)  value must be between minimum and maximum


LengthValidator
String validation, length of the String must be between minimum and maximum values

<f:validateLength minimum= maximum= />

Example :

<h:messages /> and label for <h:inputText are used to display error messages
This example will verify that the name entered must be between 3, and 20 characters

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:h="http://java.sun.com/jsf/html"
    
      >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test Validation</title>
</head>
<body>
<h:messages/>
<f:view>

<h:form id="form">
<h:outputLabel for="name" value="Enter Name"/>
<h:inputText required="true" label="name">
<f:validateLength minimum="3" maximum="20"/>
</h:inputText>
<h:commandButton action="ok" value="Validate" />
</h:form>
</f:view>
</body>
</html>


Display
If a String of a length less that 3 or maximum is entered, submission is ignored and an error message is displayed

Note:
It's possible to use only minimum, maximum or both

<f:validateDoubleRange minimum="" maximum=""

Example :

<h:messages /> and label are for displaying errors
Validator will verify if the entered double is in the specified range

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:h="http://java.sun.com/jsf/html"
    
      >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test Validation</title>
</head>
<body>
<h:messages/>
<f:view>

<h:form id="form">
<h:outputLabel for="name" value="Enter Name"/>
<h:inputText required="true" label="name">
<f:validateLength minimum="3" maximum="20"/>
</h:inputText>
<h:outputLabel for="double" value="Enter Double Value"/>
<h:inputText required="true" label="double">
<f:validateDoubleRange minimum="2.0" maximum="20.1" />
</h:inputText>
<h:commandButton action="ok" value="Validate" />
</h:form>
</f:view>
</body>
</html>


Display
First the entered value is less than the maximum, no problems. Then the entered value is Superior than the maximum.
 


<f:validateLongRange
This one will validate the entered long and verify if it's in the range

Example

<h:messages /> and label are for displaying errors

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:h="http://java.sun.com/jsf/html"
    
      >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test Validation</title>
</head>
<body>
<h:messages/>
<f:view>

<h:form id="form">
<h:outputLabel for="name" value="Enter Name"/>
<h:inputText required="true" label="name">
<f:validateLength minimum="3" maximum="20"/>
</h:inputText>
<h:outputLabel for="double" value="Enter Double Value"/>
<h:inputText required="true" label="double">
<f:validateDoubleRange minimum="2.0" maximum="20.1" />
</h:inputText>

<h:outputLabel for="long" value="Enter Long Value"/>
<h:inputText required="true" label="long">
<f:validateLongRange minimum="0" maximum="10" />
</h:inputText>

<h:commandButton action="ok" value="Validate" />
</h:form>
</f:view>
</body>
</html>


Display
In the first attempt nothing, the value is in the range specified
Second error message is displayed because value is greater than the maximum
Third, what display we'll get, if the value is not at all a Long?

0 comments:

Post a Comment

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP