4

Password should not contain username or parts of the user's full name that exceed two consecutive characters

If username= 1Abcd234 password= aBc15 then it should return error like password could not contain any parts of username.

Because "abc1" is present in both password and username.

Nishu Kashyap
  • 43
  • 1
  • 5

2 Answers2

0

I think you could use something like this:

function FindIntersectionFromStart(a,b){
    for(var i=a.length;i>0;i--){
        d = a.substring(0,i);
        j = b.indexOf(d);
        if (j>=0){
            return ({position:j,length:i});
        }
    }

    return null;
}

function FindIntersection(a,b){
    var bestResult = null;
    for(var i=0;i<a.length-1;i++){
        var result = FindIntersectionFromStart(a.substring(i),b);
        if (result){
            if (!bestResult){
                bestResult = result;
            } else {
                if (result.length>bestResult.length){
                    bestResult = result;
                }
            }
        }
        if(bestResult && bestResult.length>=a.length-i)
            break;
    }
    return bestResult;
}

var username = "myUsername";
var password = "myuse";

result = FindIntersection(username.toLowerCase(), password.toLowerCase());

if(result.length > 2){
  console.log("Invalid Password!");
} else {
  console.log("Valid Password!")
}
Derek Brown
  • 4,232
  • 4
  • 27
  • 44
0

Here's a simple approach (may not be the best though):

  1. First get all combinations of consecutive characters from username that should not be found in password. This would depend on the number of consecutive characters that your application considers invalid if matched.
  2. Now, run a simple loop and check the presence of any of the invalid combinations using String.indexOf method.
  3. Make sure that you convert both - username and password to same case for simplicity.

$("#check").on("click", checkValidity );

function checkValidity(){
  var numConsecutiveChars = 2;
  var username = $("#username").val().trim().toLowerCase();
  var password = $("#password").val().trim().toLowerCase();
  
  // first find all combinations that should not be found in password
  var invalidCombinations = [];
  for( var i = 0; i < username.length - numConsecutiveChars; i++ ){
    var curCombination = username[i] + username[i+1];
    invalidCombinations.push( curCombination );
  }//for
  
  // now check all invalidCombinations
  var invalid = false;
  for( var i = 0; i < invalidCombinations.length; i++ ){
    var curCombination = invalidCombinations[i];
    if( password.indexOf( curCombination ) !== -1 ){
      invalid = true;
      break;
    }
  }//for()
  
  if( invalid ){
    alert("Invalid password");
  }else{
    alert("Valid password");
  }
}//checkValidity()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="username" value="user" />
<input type="text" id="password" value="pass" />
<button id="check">Check validity</button>
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64