2

I am working on an ATG project. My requirement is "the password should not contain the same sequence of characters as in UserName". For eg. If the userName is abcdef@123.com. Then pw should not be def@123.com The pw should not include same SEQUENCE of characters. It can have the characters as in userName. But the ordering/sequence of characters shouldnot be the same.

How can I check this?

Thanks, Treesa

Neenu
  • 59
  • 3
  • 7
  • 2
    This sounds like a dubious requirement. If my username is 'Treesa@stackoverflow.com' even my password can't be 'TopSecret' as it contains an 'r' and 'e' in sequence. – radimpe Feb 27 '15 at 10:14

2 Answers2

1

You can do the following to check if the password contains any sequence of the username :

public Boolean containsSequences(String uname, String pwd){
  Boolean contains=false;
  int count=0;
  for (String seq: uname.split("/[\@\-\.\_]/g")){ //split the username following this regex.
     if(pwd.indexOf(seq)>0){
        count++;
     }
  }
  if(count>0){
     contains=true;
  }
  return contains;
}

This method takes two strings(username and pwd) in input, then takes the sub-sequences of the username and test if the pwd contains one of them, if so then return true menas that the pwd contains a sequence of the username.

Or much better you could do:

 public Boolean containsSequences(String uname, String pwd){
  Boolean contains=false;
  for (String seq: uname.split("/[\@\-\.\_]/g")){ 
     if(pwd.contains(seq)|| pwd.contains(seq.toUpperCase())|| pwd.contains(seq.toLowerCase())){
        contains=true;
        break;
     }
  }
  return contains;
}
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
-2

use passowrdString.contains(userNameString)

user93796
  • 18,749
  • 31
  • 94
  • 150
  • This will only confirm that the password contains the entire username, which is not what the original question states. – radimpe Feb 27 '15 at 10:10