Java Reference
In-Depth Information
private String phone;
@Pattern(regexp="[a-z0-9!#$%&'*+/
=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/
=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",message="Invalid
email")
//if the field contains email address
consider using this//annotation to enforce
field validation
@Size(max = 60)
@Email
private String email;
//...
}
Once the constraints are defined, the Bean Validation Specification allows you to
validate the data under constraints manually or automatically through other specific-
ations. We begin by presenting manual validation. The following example demon-
strates how to validate the constraints of a class manually. We must say that the
Validator API also provides methods to validate a single attribute or a specific
value as shown in the following code:
public static void main(String[] args) {
Student student = new Student();
student.setEmail("qsdfqsdfqsdfsqdfqsdfqsdf");
student.setPhone("dfqsdfqsdfqsdfqsdfqsdfqsd");
ValidatorFactory factory
=Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Student>> violations
=validator.validate(student);
System.out.println("Number of violations :
"+violations.size());
for(ConstraintViolation<Student> cons :
violations){
Search WWH ::




Custom Search