Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Bean validation: Email: validate email

If you annotate @Email annotation on top of CharSequence, then the CharSequence should be a well-formed email address.




public String emailId;


What are the supported types?

You can apply this annotation on any CharSequence type.


Where can I apply this annotation?

a.   METHOD, F

b.   IELD,




f.     TYPE_USE


Find the below working application.

package com.sample.model;

import javax.validation.constraints.Email;

public class Employee {

private int id;

private String name;

public String emailId;

public Employee(int id, String name, String emailId) { = id; = name;
this.emailId = emailId;

public int getId() {
return id;

public void setId(int id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;

public String getEmailId() {
return emailId;

public void setEmailId(String emailId) {
this.emailId = emailId;


package com.sample.test;

import java.util.Set;

import javax.validation.*;
import javax.validation.ValidatorFactory;

import com.sample.model.Employee;

public class Test {
private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
private static Validator validator = validatorFactory.getValidator();

private static void validateBean(Employee emp) {
Set> validationErrors = validator.validate(emp);

if (validationErrors.size() == 0) {
System.out.println("No validation errors....");

for (ConstraintViolation violation : validationErrors) {
System.out.println(violation.getPropertyPath() + "," + violation.getMessage());

public static void main(String args[]) {

Employee emp1 = new Employee(1, "Krishna", "AA");
System.out.println("Validation errors on bean emp1");

Employee emp2 = new Employee(2, "Siva", "[email protected]");
System.out.println("Validation errors on bean emp2");



Validation errors on bean emp1
emailId,must be a well-formed email address

Validation errors on bean emp2
No validation errors....






Previous                                                    Next                                                    Home

This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Bean validation: Email: validate email


Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription