Common business validation scenarios not provided out of the box by the JSF (1/1.2/2) RI implementation are provided by the Apache Commons Validator for JSF.

At which point, you don’t have to be using MyFaces implementation of the JSF JSR to be using the MyFaces Commons Validator library. For example, i am using it with Oracle Sun Mojarra JSF implementation.

Common business validation scenarios:

  • Email validation * (Guess this is most popular)
  • Credit Card number validation
  • Book ISBN validation
  • URL validation
  • CSV validation
The complete list can be found here:
The MyFaces Commons works for most versions of JSF:
  • 1.1
  • 1.2
  • 2.0

Maven Dependency:

(This is for JSF2)

<!-- MyFaces Commons Validators (JSF) -->
<dependency>
    <groupId>org.apache.myfaces.commons</groupId>
    <artifactId>myfaces-validators20</artifactId>
    <version>1.0.2</version>
</dependency>

Repository Manager:

Maven Central: http://search.maven.org/#artifactdetails%7Corg.apache.myfaces.commons%7Cmyfaces-validators20%7C1.0.2%7Cjar

Usage:

1. Add the bold tag information to your html declaration header.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
	  xmlns:f="http://java.sun.com/jsf/core"
	  xmlns:p="http://primefaces.prime.com.tr/ui"
	  xmlns:mcv="http://myfaces.apache.org/commons/validators">

2. Add the validator tag to your email input field of your XHTML source file:

<h:outputText value="Reservation" />
<p:inputText id="receptionEmail" required="true">
    <mcv:validateEmail />
</p:inputText>

3. The result:

Share