java.net member

Rechercher dans ce site

Showing posts with label JSF - Language. Show all posts
Showing posts with label JSF - Language. Show all posts

JSF Validation - Part 2

>> 15 May 2010

This tutorial works on JSF 2 using MyFaces 2
<f:validateRegex
This is a JSF 2  standard validator.
This validator provides reqular expression-based validation
A validator that valid the component's value against the "pattern" attribute
You specify a regular expression (java one) in pattern.

In testValidation.xhtml
<code>
<?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:outputText for="name" value="Enter your name with small letters"/>
<h:inputText id="name" required="true" label="name">
  <f:validateRegex pattern="[a-z]+"/>
</h:inputText>
              

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

Display
No error message if lower case letters

Error message with upper case

Note:
You can verify or valid normally any regular expression here. If you try to validate a complex pattern (like e-mail), you'll get a complex error message too. One of the solutions will be to write your own error message in a properties file (message.properties for instance),put it in the package with classes. Tell faces-config.xml to load the file.

message.properties

javax.faces.validator.RegexValidator.NOT_MATCHED="E-Mail is not valid"


faces-config.xml

<code>
<faces-config>
....
....
<application>
<message-bundle>fr.iipt.ka.faces.messages</message-bundle>
</application>
....
....
</faces-config>
</code>

In testValidation.xhtml

<code>

<?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:outputText for="email" value="Enter your e-mail"/>
                <h:inputText id="email" required="true" label="email">
                <f:validateRegex pattern="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$"/>
                </h:inputText>
               

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


Display
First attempt with entered e-mail does not match "Regex"

And one that matched

Read more...

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?

Read more...

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

Back to TOP