title: Custom validator for a phone number in JSF
body: <p>i am trying to do some validation on a phone number although i understand how to do validation for just numbers of a set size this task is a little tricky </p>
  
  <p>i am required to have validation on a users phone number however it must be  in any one of the formats illustrated below (where spaces are not significant):</p>
  
  <pre><code>+44 (23) 92846438
  (023) 92846438
  </code></pre>
  
  <p>how would i create a custom validation that would validate against all combinations of the above phone numbers ?</p>
  
  <p>Thanks</p>
  
  <p>i have tried created a backing bean and validating through that</p>
  
  <pre><code>public class PhoneValidator implements Validator{
  
  private static final String phone_PATTERN = "^[0-9-]{12,15}$";
  
      private Pattern pattern;
      private Matcher matcher;
  
      public UserNameValidator(){
            pattern = Pattern.compile(phone_PATTERN);
      }
  
      @Override
      public void validate(FacesContext context, UIComponent component,
              Object value) throws ValidatorException {
  
          matcher = pattern.matcher(value.toString());
          if(!matcher.matches()){
  
              FacesMessage msg = 
                  new FacesMessage("Phone validation failed", 
                          "Invalid phone number.");
              msg.setSeverity(FacesMessage.SEVERITY_ERROR);
              throw new ValidatorException(msg);
  
          }
  
      }
  }
  </code></pre>
  
  <p>would something like this work :</p>
  
  <pre><code>private static final String phone_PATTERN = "^[+][44][(0-9)][0-9-]{12,15}$";
  </code></pre>
  
  <p>and if so how can i get it to check both methods </p>
  
  <p>edit, i have tried the above but it does not work, i think i am on the right lines here just can not get the combination right</p>