I need to validate a field - secPhoneNumber (secondary phone #). I need to satisfy below conditions using JSR validation
- The field can be empty/null
- Otherwise, the data must be of length 10.
I tried the code below. The field is always getting validated on form submission. How do I validate the field to be of length 10 only when it is not empty?
Spring Form:
<form:label path="secPhoneNumber">
Secondary phone number <form:errors path="secPhoneNumber" cssClass="error" />
</form:label>
<form:input path="secPhoneNumber" />
Bean
@Size(max=10,min=10)
private String secPhoneNumber;