Skip to content

Instantly share code, notes, and snippets.

@dbrack
Last active July 19, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrack/d6ee62132cf109ebfedc03cb16510779 to your computer and use it in GitHub Desktop.
Save dbrack/d6ee62132cf109ebfedc03cb16510779 to your computer and use it in GitHub Desktop.
Simple password validation
const validatePassword = (pw: string) => {
const minimumLength = 5;
const amountOfCriteria = 1;
const isLongEnough = (pw: string) => pw.length >= minimumLength;
const containsDigit = (pw: string) => (
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
.some(n => pw.includes(String(n)))
);
return Array.of(isLongEnough, containsDigit)
.map(f => f(pw))
.filter(fulfilled => fulfilled)
.length > amountOfCriteria;
}
alert(`password fulfills requirements: ${validatePassword('veryC0mplex')}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment