1/15/2017 - 9:42 PM

Customized Validation

Customized Validation must be between {2} and {1} characters
Email=Not a valid email address
NotEmpty=Field cannot be left blank
Phone=Not a valid phone number Name Email Address Phone
package com.pluralsight.view;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PhoneConstraintValidator implements ConstraintValidator<Phone, String> {

	public void initialize(Phone phone) {

	public boolean isValid(String phoneField, ConstraintValidatorContext cxt) {
		if(phoneField == null){
			return false;
		return phoneField.matches("[0-9()-]*");

package com.pluralsight.view;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

public @interface Phone {
	String message() default "{Phone}";
	Class<?>[] groups() default{};
	Class<? extends Payload>[] payload() default{};
package com.pluralsight.model;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import com.pluralsight.view.Phone;

public class Attendee {
	@Size(max=30, min=2)
	private String name;
	@NotEmpty @Email
	private String emailAddress;
	private String phone;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getEmailAddress() {
		return emailAddress;
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	public String getPhone() {
		return phone;
	public void setPhone(String phone) { = phone;