Created
August 26, 2018 16:19
-
-
Save ThakurPriyanka/0ecbbc174032ce5ca4edf3de8db10846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.knoldus.user.annotation; | |
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
public class PasswordValidation implements ConstraintValidator<Password, String> { | |
private static final boolean NUMBER_OF_REQUIRED_DIGITS = true; | |
public static boolean validate(String password) { | |
if (password == null || password.length() == 0) { | |
return true; | |
} else if (password.length() < 10 || password.length() > 15) { | |
return false; | |
} | |
boolean numberOfDigits = false; | |
for (char p : password.toCharArray()) { | |
if (Character.isDigit(p)) { | |
numberOfDigits = true; | |
} else if (Character.isSpaceChar(p)) { | |
return false; | |
} | |
} | |
return (numberOfDigits == NUMBER_OF_REQUIRED_DIGITS); | |
} | |
public void initialize(Password gap) { | |
// No-op. | |
} | |
public boolean isValid(String password, ConstraintValidatorContext cac) { | |
// The password field should be annotated with @NotNull, so we return true because | |
// lack of a (null) password field is valid in certain validation scenarios. | |
return password == null || validate(password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment