@Test public void validate() { Assert.assertThat(invokeValidator("0123456789"), is(true)); Assert.assertThat(invokeValidator("9876543210"), is(true)); Assert.assertThat(invokeValidator("012345678"), is(false)); Assert.assertThat(invokeValidator("01234567891"), is(false)); Assert.assertThat(invokeValidator("0123x56789"), is(false)); }
@Override public void validate(Object o, Errors errors) { String number = (String) o; number = number.trim(); if (number.length() != 10) { errors.rejectValue(null, "Phone numbers must be 10 digits long"); } if (!number.matches("\\d{10}")) { errors.rejectValue(null, "Phone numbers must only contain numbers"); } }
TelephoneNumberValidator implements Validator { @Override public void validate(Object o, Errors errors) { String number = (String) o; number = number.trim(); if (number.length() != 10) { errors.rejectValue(null, "Phone numbers must be 10 digits long"); } if (!number.matches("\\d{10}")) { errors.rejectValue(null, "Phone numbers must only contain numbers"); } } }
TelephoneNumberValidator implements Validator { @Override public void validate(Object o, Errors errors) { String number = (String) o; number = number.trim(); if (number.length() != 10) { errors.rejectValue(null, "Phone numbers must be 10 digits long"); } if (!number.matches("\\d{10}")) { errors.rejectValue(null, "Phone numbers must only contain numbers"); } }  }
TelephoneNumberValidator implements Validator { @Override public void validate(Object o, Errors errors) { String number = (String) o; number = number.trim(); if (number.length() != 10) { errors.rejectValue(null, "Phone numbers must be 10 digits long"); } if (!number.matches("\\d{10}")) { errors.rejectValue(null, "Phone numbers must only contain numbers"); } }  @Override boolean supports(Class<?> clazz); @Override void validate(Object o, Errors errors); }
TelephoneNumberValidator implements Validator { @Override public void validate(Object o, Errors errors) { String number = (String) o; number = number.trim(); if (number.length() != 10) { errors.rejectValue(null, "Phone numbers must be 10 digits long"); } if (!number.matches("\\d{10}")) { errors.rejectValue(null, "Phone numbers must only contain numbers"); } }  @Override boolean supports(Class<?> clazz); @Override void validate(Object o, Errors errors);  }