title: Java bean validation: Enforce a @Pattern only when the property is not blank
body: <p>I have a form that allows the user to optionally enter their zip code. I know how to validate the format of the zip code, by using a <code>@Pattern</code> constraint and regex. But as this is an optional field, I do not want the format validated if it is blank.</p>
  
  <p><strong>How can I tell the system to ignore the <code>@Pattern</code> constraint if the field is blank?</strong> This is an optional field and I only want to validate it if it is not blank.</p>
  
  <pre><code>public class MyForm {
      String firstName;
      String lastName;
  
      @Pattern(regex = "^\d{5}(?:[-\s]\d{4})?$")
      String optionalZipCode;      // &lt;- This is an optional field.
  }
  </code></pre>