Visit GitHub RepoValidation

User Validator

Library to validate username, email and passwords.

Java library to validate typical user fields. Useful for validation when registering new users or logging in.

#Show me the code!!!

First, instance the validator:

UserValidator validator = UserValidator.builder().build();

So, how are you using it?

try {
  if (validator.validateUsername(username) && 
    validator.validateEmail(email) && 
    validator.validatePassword(username, password)) {
      // TODO login, register or edit user
  } catch (UsernameIsNullException exception) { // Catch all exceptions you're interested to handle
      // TODO show something to the user!!!


  @Test (expected = InvalidEmailFormatException.class)
  public void shouldThrowInvalidEmailFormatExceptionWhenEmailHasInvalidFormat() throws Exception {

  @Test (expected = NullEmailException.class)
  public void shouldThrowInvalidEmailFormatExceptionWhenEmailIsNull() throws Exception {

  @Test (expected = InvalidPasswordException.class)
  public void shouldThrowInvalidPasswordExceptionWhenInvalidPassword() throws Exception {
    validator.validatePassword("username", "username");

  @Test (expected = InvalidPasswordFormatException.class)
  public void shouldThrowInvalidFormatPasswordExceptionWhenPasswordHasInvalidCharacters() throws Exception {
    validator.validatePassword("username", "pass word");

  @Test (expected = InvalidPasswordLengthException.class)
  public void shouldThrowInvalidLengthPasswordExceptionPasswordIsTooLong() throws Exception {
    validator.validatePassword("username", "passwordWithMoreThanTwentyCharacters");

  @Test (expected = InvalidPasswordLengthException.class)
  public void shouldThrowInvalidLengthPasswordExceptionPasswordIsTooShort() throws Exception {
    validator.validatePassword("username", "hi");

  @Test (expected = NullPasswordException.class)
  public void shouldThrowNullPasswordExceptionPasswordIsNull() throws Exception {
    validator.validatePassword("username", null);

  @Test (expected = UsernameIsNullException.class)
  public void shouldThrowUsernameIsNullExceptionWhenUsernameIsNull() throws Exception {

  @Test (expected = InvalidUsernameFormatException.class)
  public void shouldThrowUsernameIsNullExceptionWhenUsernameHasInvalidCharacters() throws Exception {
    validator.validateUsername("user name");

  @Test (expected = InvalidUsernameLengthException.class)
  public void shouldThrowInvalidUsernameLengthExceptionWhenUsernameIsTooLong() throws Exception {

  @Test (expected = InvalidUsernameLengthException.class)
  public void shouldThrowInvalidUsernameLengthExceptionWhenUsernameIsTooShort() throws Exception {

#How are your default validations?

private static final String EMAIL_PATTERN = "[a-zA-Z0-9\\+\\._%\\-\\+]{1,256}" +
        "@" +
        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
        "(" +
        "\\." +
        "[a-zA-Z0-9][a-zA-Z0-9\\-]{1,25}" +
private static final String USERNAME_PATTERN = "^([-_A-Za-z0-9])*$";
private static final int USERNAME_MINIMUM_LENGTH = 3;
private static final int USERNAME_MAXIMUM_LENGTH = 25;
private static final int PASSWORD_MINIMUM_LENGTH = 6;
private static final int PASSWORD_MAXIMUM_LENGTH = 20;
private static final String PASSWORD_PATTERN = "^([A-Za-z0-9_.,&%€@#~])*$";

#Can I customize my validations?

Sure, you can do something like this:

        .emailPattern("here be email pattern")
        .passwordPattern("here be password pattern")
        .usernamePattern("here be username pattern")

#I want to use it!!!

  • Grab via Gradle:
compile 'com.shootr:user-validator:1.0.0'
  • Grab via Maven:

#I want to contribute!!!

You are welcome! Please feel free to do Pull Requests or reporting issues.

#Who did this???

Arturo Jiménez

Become a better Android Developer
Millions of developers are learning at MindOrks

Online Training Program

Featured Blogs

Our Team

MindOrks is Certainly one of the best online blog to stay on top of all the Android development news, coding and design patterns. Finally a blog I can count on to keep myself updated with latest and greatest things happening in Android world.

Vipul Shah
Android Developer

Become Pro in Android by watching videos