Spring Validation Example @NotEmpty + @NotNull + @DateTimeFormat + @NumberFormat + @Pattern

 

Spring Validation Annotation Examples

 

 Annotation

Example   Description  package
@NotEmpty @NotEmpty
private String firstName;
Checks if:
 object != null
org.hibernate.validator.constraints.NotEmpty
@NotNull @NotNull
private Boolean isStudent;
Checks if:
 object != null and
 object size >= 1
javax.validation.constraints.NotNull
@Size @Size(min=1,max=20)
private String lastName;
Checks for min and max size  javax.validation.constraints.Size
@DateTimeFormat @DateTimeFormat(pattern = "MM/dd/yyyy")
private Date dob;
The custom patternto use to format the field. This is used for display purpose on the form too. org.springframework.format.annotation.DateTimeFormat
@NumberFormat @NumberFormat(pattern="#.##")
private Double weight;
The custom patternto format the Double value.  org.springframework.format.annotation.NumberFormat
@Pattern @Pattern(regexp="^[A-Za-z]*$")
private String lastName;
Support to Regular expressions. In this example restricts to lower and upper case characters only.  javax.validation.constraints.Pattern
@NotBlank @NotEmpty
private String firstName;
 
Checks the size after trimming the String. Do not use to check for null.   org.hibernate.validator.constraints.NotBlank
Web Analytics