In this tutorial you will learn about how to use the JSF 2 validateRegex tag to validate the wrapping component.
JSF 2 f validateRegex
In this tutorial you will learn about how to use the JSF 2 validateRegex tag to validate the wrapping component.
<f:validateRegex> tag is a newly added into the JSF 2 core tag library. This tag validates the component's value according to the defined pattern inside which it is wrapped. The value of Component will be valid if its value is matched with the given entire pattern.
Attributes of <f:validateRegex>
- disabled : This attribute is not required to be used with
this tag. This attribute evaluates to a java.lang.Boolean which value specifies
whether this tag will be enabled or not for the component on the JSF page.
- pattern : This attribute is required to be used with this
tag. This attribute specifies the pattern for the component value to must match
with the regular expression pattern.
- binding : This attribute is not required to be used with this tag
but, is used to bound the expression to evaluate to javx.faces.validator.RegexValidator instance.
- for : This is not a required attribute to be used with this tag. This attribute is used to refer one of the composite component's value within which this tag is nested.
Example :
Here an example is being given for you which will demonstrate you about how to use the JSF 2 f validateRegex tag in your application. In this example at first I have created a JavaBeans with the property 'name' and its setter getter method. Then created the JSF pages 'inputName.xhtml' for taking the input in the textbox using the <h:inputText> tag and inside this tag used the <f:validateRegex> tag to specify the pattern that must be given the input in the textbox and also used the <h:message> tag to show the error message if the input value doesn't match the pattern and created an 'outputName.xhtml' to show the output for the input value given into the textbox.
Directory Structure
Here is the directory structure for this example
NameValidationBean.java
package devmanuals; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name="nameValidation") @RequestScoped public class NameValidationBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
inputName.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <head> <title>JSF f validateRegex example</title> </head> <body> <f:view> <h:form> Enter your email id : <h:inputText id="tx" value="#{nameValidation.name}"> <f:validateRegex pattern="[dD]eepak" /> </h:inputText><br/> <h:message for="tx" style="color:red"/><br/> <h:commandButton value="submit" action="outputName"/> </h:form> </f:view> </body> </html>
outputName.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <head> <title>output email</title> </head> <body> <f:view> Welcome, <h:outputText value="#{nameValidation.name}"></h:outputText> </f:view> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>jsfValidateRegex</display-name> <welcome-file-list> <welcome-file>inputName.xhtml</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> <url-pattern>*.jsf</url-pattern> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>resources.application</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> </web-app>
How to Run
At first start your server then use the following link http://localhost:8181/jsfValidateRegex/inputName.jsf in the address bar of your web browser
Output
When you will execute the above example you will get the output as follows :
1. Then main page will be opened as follows :
2. When you will give the value in the textbox as "deepak/Deepak" according to the specified pattern of <f:validateRegex> then the outpuName.xhtml page will be rendered and the output will be showed even if you don't give any value in the textbox the outputName.xhtml page will be rendered but will not show the dynamic(#{nameValidation.name}) value
3. But if you will give the value other than the specified pattern then the output will be as follows :
[ 0 ] Comments