Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active December 21, 2015 23:48
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 yckart/6384443 to your computer and use it in GitHub Desktop.
Save yckart/6384443 to your computer and use it in GitHub Desktop.
Password Strength
function (a) {
return (
(a.length >= 8) + // length
/[a-z]/.test(a) + // lowercase
/[A-Z]/.test(a) + // uppercase
/\d/.test(a) + // number
/[^\w\s]|_/.test(a) // special
) * 20
};
function(a){return((a.length>=8)+/[a-z]/.test(a)+/[A-Z]/.test(a)+/\d/.test(a)+/[^\w\s]|_/.test(a))*20}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Yannick Albert <http://yckart.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "passwordStrength",
"description": "Checks a string for its password strength.",
"keywords": [
"password",
"strength",
"indicator",
"regexp",
"string"
]
}
<!DOCTYPE html>
<title>Foo</title>
<input id="input">
<div id="ret"></div>
<script>
var strength = function(a){return((a.length>=8)+/[a-z]/.test(a)+/[A-Z]/.test(a)+/\d/.test(a)+/[^\w\s]|_/.test(a))*20};
document.getElementById('input').onkeyup = function () {
document.getElementById('ret').innerHTML = strength(this.value);
};
</script>
@atk
Copy link

atk commented Aug 30, 2013

n / 5 * 100 = n * 20 and \d is shorter than [0-9]:

function(a){return((a.length>=8)+/[A-Z]/.test(a)+/[a-z]/.test(a)+/\d/.test(a)+/[^\w\s]|_/.test(a))*20}

Even shorter if you use replace inside eval (ugly, but very short):

function(a){return 20*eval("/.{8}1/[A-Z]1/[a-z]1/\\d1/[^\\w\\s]|_10".replace(/1/g,'/.test(a)+'))}

@yckart
Copy link
Author

yckart commented Aug 30, 2013

Good point! I merged your 1st suggest. And ignored the 2nd one, eval === evil and looks too ugly to me... ;)

@atk
Copy link

atk commented Aug 30, 2013

That was just to show how short it could be done. I wouldn't do something like that in real life.

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