To validate email, uses JSF <f:validateRegex>, and puts following regular expression. This regex should be able to validates most of the email format, and I’m using it for few projects.

Email Regular Expression

P.S For detail explanation, refer to this how to validate email address with regular expression.

In this tutorial, we will show you a simple email input, and uses <f:validateRegex> to validate it.

Tools used :

  1. PrimeFaces 3.3
  2. JSF 2.2.11
  3. Eclipse 4.2
  4. Maven 3
  5. Tomcat 7

1. Email Validator Example

If email is invalid, display error message “Invalid email format”.

<!DOCTYPE html>
<html xmlns=""
  <f:facet name="last">
	<h:outputStylesheet library="mytheme" name="css/style.css" />
  <h1>PrimeFaces email validator example</h1>
  <div style="width: 500px">
	<p:inputText id="email" required="true" label="email" size="40"
		requiredMessage="Please enter your email address."
		validatorMessage="Invalid email format"
		pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />
	<p:watermark for="email" value="Email Address *" />
	<p:message for="email" />
	<p:commandButton value="test" style="margin:20px"
		action="#{userBean.register}" ajax="false" />
</html> – Do nothing, if email is ok, redirect to thanks page.
package com.mkyong;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userBean")
public class UserBean {
	String email;
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;
	public String register() {
		return "thanks?faces-redirect=true";

2. Demo


email validator with regex

Display error message if email validation is failed.

Download Source Code


  1. Validate email address with regex
  2. Another great email regex example
Tags :
Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Related Posts

Popular Posts