java.net member

Rechercher dans ce site

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

2 comments:

Anonymous,  January 30, 2012 at 5:01 PM  

Hello. What if I have a lot of validateRegex in my JSF pages. if I write javax.faces.validator.RegexValidator.NOT_MATCHED="E-Mail is not valid" in my messages.properties I'll have this message to ALL validation errors. What I have to do to have different messages?

okjavafx February 2, 2012 at 9:12 AM  

Hello,
This really is a very good question ;)
You can use validatorMessage and customize it for your fields. Here is a little example:

<h:inputText value="#{bean.name}" id="name" label="#{msgs.name}" required="true" validatorMessage="#{bean.name}:#{msgs.error_name}">
<f:validateRegex pattern="xyz" />
</h:inputText>




Hope you find my answer helpful.

Kaesar

Post a Comment

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

Back to TOP