Skip to content

Instantly share code, notes, and snippets.

@MahbbRah
Last active September 19, 2019 13:23
Show Gist options
  • Save MahbbRah/0a729e929ca020774c94014af5b4cfb8 to your computer and use it in GitHub Desktop.
Save MahbbRah/0a729e929ca020774c94014af5b4cfb8 to your computer and use it in GitHub Desktop.
Check if password includes user name or part of it
function checkPasswordForUserName(userFullName, userPass) {
const userNamesList = userFullName.toLowerCase().split(" ");
const firstThreeChars = userFullName.substring(0, 3);
const lowerCasePassword = userPass.toLowerCase();
const isUserNameInPassword = (name) => (
lowerCasePassword.includes(name) ||
lowerCasePassword.includes(firstThreeChars)
);
return userNamesList.some(isUserNameInPassword);
}
//usage;; this will return true that means the password includes name of user!
checkPasswordForUserName("Mahbub Rahman", "mahbub343345");
@victormiguez
Copy link

victormiguez commented Sep 19, 2019

Hey @MahbbRah, great gist, thanks for sharing!

How about we ignore the letter case for a more precise validation?
Also, I've made some changes on the code following functional programming standards, what do you think?

function checkPasswordForUserName(userFullName, userPass) {
  const userNamesList = userFullName.toLowerCase().split(" ");
  const firstThreeChars = userFullName.substring(0, 3);
  const lowerCasePassword = userPass.toLowerCase();
  const isUserNameInPassword = (name) => (
    lowerCasePassword.includes(name) ||
    lowerCasePassword.includes(firstThreeChars)
  );
  
  return userNamesList.some(isUserNameInPassword);
}

@MahbbRah
Copy link
Author

Hey @MahbbRah, great gist, thanks for sharing!

How about we ignore the letter case for a more precise validation?
Also, I've made some changes on the code following functional programming standards, what do you think?

function checkPasswordForUserName(userFullName, userPass) {
  const userNamesList = userFullName.toLowerCase().split(" ");
  const firstThreeChars = userFullName.substring(0, 3);
  const lowerCasePassword = userPass.toLowerCase();
  const isUserNameInPassword = (name) => (
    lowerCasePassword.includes(name) ||
    lowerCasePassword.includes(firstThreeChars)
  );
  
  return userNamesList.some(isUserNameInPassword);
}

This one super cool!, Thanks for the improvement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment