0

I need to have following password validations :

  1. At least Min Characters 8 and Maximum Characters 15
  2. At least One Number and 1 special characters from (! @#$%^&*-=+?.);
  3. At least One lower case letter
  4. Password shouldn't be sub strings of username and email (min length 3 and max length 15).
  5. Password should be case sensitive.

I have also looked these answers but I am confuse , should I use Input filters to achieve this or Regex?

Any help will be appreciable. It will be great if you guyz provide a working solution.

4
  • You looked at that answer and did you try it? Commented Nov 28, 2016 at 8:03
  • Yes I tried but it isn't fulfilling my complete requirement. @WiktorStribiżew could you please help me with it. Commented Nov 28, 2016 at 8:07
  • Sorry, I have no idea how you can check Req. 4 with a regex. Commented Nov 28, 2016 at 8:08
  • @WiktorStribiżew any help for rest points expect 4 Commented Nov 28, 2016 at 12:53

3 Answers 3

4
public class Validation {

    public static void main(String[] args) {  
       String pass = "1AB%CDef555";
       String username = "manna";
       String email = "[email protected]";
       System.out.println(validiate2(pass, username,email));
    }
    // if you don't care why it fails and only want to know if valid or not 
    public static boolean validiate (String pass, String username, String email){
        String pattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$";
        if(pass.matches(pattern)){
            for(int i=0;(i+3)<username.length();i++){
                if(pass.contains(username.substring(i,i+3)) || username.length()<3 || username.length()>15){
                   return false; 
                }                  
            }
            for(int i=0;(i+3)<email.length();i++){
                if(pass.contains(email.substring(i,i+3)) || email.length()<3 || email.length()>15){
                   return false; 
                }                  
            }
            return true;
        }
        return false;    
    }
    // if you want to know which requirement was not met
    public static boolean validiate2 (String pass, String username, String email){
        if (pass.length() < 8 || pass.length() >15 ){
            System.out.println("pass too short or too long");
            return false;
        }
        if (username.length() < 3 || username.length() >15 ){
            System.out.println("username too short or too long");
            return false;
        }
        if (!pass.matches(".*\\d.*")){
            System.out.println("no digits found");
            return false;
        } 

        if (!pass.matches(".*[a-z].*")) {
            System.out.println("no lowercase letters found");
            return false; 
        }
        if (!pass.matches(".*[!@#$%^&*+=?-].*")) {
            System.out.println("no special chars found");
            return false; 
        } 
        if (containsPartOf(pass,username)) {
            System.out.println("pass contains substring of username");
            return false; 
        }
        if (containsPartOf(pass,email)) {
            System.out.println("pass contains substring of email");
            return false; 
        }
        return true;           
    }

    private static boolean containsPartOf(String pass, String username) {
        int requiredMin = 3
        for(int i=0;(i+requiredMin)<username.length();i++){
            if(pass.contains(username.substring(i,i+requiredMin))){
               return true; 
            }                  
        }
        return false;        
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is great library for that.
It's uses anotations for field and has rich customatization. I think that #4 still needs to be done by hand but you should definitly check out the library.

Here's the example from github:

@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
private EditText passwordEditText;

Cheers.

1 Comment

Thanks @makvasic , but I'm actually not looking for a lib to achieve this
0

You can try this one:

^(?!.*(user|emailaddress))(?=.*\d)(?=.*[! @#$%^&*=+?.-])(?=.*[a-z]).{8,15}$

Make sure you replace the user and emailaddress by your variable

Explanation

This code works fine for me:

public class NewClass1 {
    public static void main(String[] args) {
        NewClass1 nc = new NewClass1();
        nc.check("abcd123-", "userName", "[email protected]");
        nc.check("userName1-", "userName", "[email protected]");
        nc.check("[email protected]", "userName", "[email protected]");
        nc.check("abcy.c1b", "userName", "[email protected]");
        nc.check("abcd123-", "userName", "[email protected]");
    }

    public void check(String string, String userName, String email) {

        final String regex = "^(?!.*(" + userName + "|" + email + "))(?=.*\\d)(?=.*[! @#$%^&*=+?.-])(?=.*[a-z]).{8,15}$";
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
        if (matcher.find()) {
            System.out.println(string + "Full match: " + matcher.group(0));
        } else {
            System.out.println("no match");
        }
    }
}

5 Comments

@Maverick_Mrt Even replacing user|emailaddress with my values, it still doesn't completing 4th point.
@rahulsharma regex101.com/r/pN5mWQ/3 look here .. if the user name is 'user' or the email address is 'email' it doesn't match ... just tell me by editing in the link if it is working or not ... then i will look into your code
@rahulsharma please have a look in my updated answer. having code snippet... let me know if it works for you
Dear @maverick_Mrt there's shouldn't be substring min 3 or max 15 character , your answer works only when "user" or "email" is entered , it fails if inputs "use@1234" , see regex101.com/r/pN5mWQ/3
in your original question you wrote, At least Min Characters 8 and Maximum Characters 15 ... moreover, use my code and append the following line ... nc.check("use@1234-", "userName", "use@1234"); it will say no match... it should say no match isn't it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.