title: JSR-303 Bean Validation - Custom Constraint Multiple Annotations to One Validator
body: <p>When writing custom constraints is it possible to have multiple annotations validated by one validator implementation.  For example I have several annotations which stipulate different @size annotations but I want them all to point at the same validator class which does some global checking i.e. all must be match a certain regex.  As far as I can see the implementation takes in one Annotation Type.</p>
  
  <p>One annotation</p>
  
  <pre><code>@Target( { METHOD, FIELD, ANNOTATION_TYPE, TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy = {UCNValidator.class})
  @Documented
  @Size(min = 9, max = 9, message = "{exactlength}")
  public @interface UCN {
  
      String message() default "{invalidFormat}";
  
      Class&lt;?&gt;[] groups() default {};
  
      Class&lt;? extends Payload&gt;[] payload() default {};
  
      String fieldName() default "ucn";
  
  }
  </code></pre>
  
  <p>The validator</p>
  
  <pre><code>public class UCNValidator implements ConstraintValidator&lt;UCN, String&gt;
  {
  
      private String pattern = "[a-zA-Z].*";
      private String fieldName;
  
      @Override
      public void initialize( UCN constraintAnnotation )
      {
          this.fieldName = constraintAnnotation.fieldName();
      }
  
      @Override
      public boolean isValid( String value, ConstraintValidatorContext constraintValidatorContext )
      {
  
          if ( value != null )
          {
              if ( !value.matches(pattern) )
              {
                  //do some stuff
                  return false;
              }
  
          }
  
          return true;
  
      }
  </code></pre>