Skip to content

Instantly share code, notes, and snippets.

@randy-johnson
Last active September 25, 2021 17:15
Show Gist options
  • Save randy-johnson/0201aebe4a4680c752a52a2ba84f3858 to your computer and use it in GitHub Desktop.
Save randy-johnson/0201aebe4a4680c752a52a2ba84f3858 to your computer and use it in GitHub Desktop.
CFML PasswordCheck
<cfscript>
/* Based on the passwordCheck tag version by Scott Stroz
https://cflib.org/udf/passwordCheck
Handy Dandy character class cheat sheet
https://www.petefreitag.com/cheatsheets/regex/character-classes/
*/
function passwordCheck(required string password, string CharOpts="alpha,digit,punct,lower,upper", numeric typesRequired="5",numeric length='8' ){
/* Initialize variables */
var TypesCount = 0;
var i = "";
var charClass = "";
var checks = {};
var numReq = "";
var reqCompare = "";
var j = "";
/* Use regular expressions to check for the presence banned characters such as tab, space, backspace, etc and password length */
if ( ReFind("[[:cntrl:] ]",password) OR len(password) LT length) {
return false;
}
/*Loop through the list 'mustHave'*/
cfloop( list="#charOpts#" index="i") {
var charClass = listGetat(i,1,' ');
/* Check to see if item in list should be included or excluded */
if (listgetat(i,1,"_") eq "no") {
var regex = "[^[:#listgetat(charClass,2,'_')#:]]";
} else {
var regex = "[[:#charClass#:]]";
}
/* If regex found, set variable to position found */
var checks["check#replace(charClass,' ','_','all')#"] = ReFind(regex,password);
/* If regex not found set valid to false */
if( checks["check#replace(charClass,' ','_','all')#"] GT 0) {
var typesCount = typesCount + 1;
}
if (listLen(i, ' ') GT 1) {
var numReq = listgetat(i,2,' ');
var reqCompare = 0;
cfloop( from="1" to="#len(password)#" index="j") {
if (REFind(regex,mid(password,j,1))){
var reqCompare = reqCompare + 1;
}
}
if ( reqCompare LT numReq) {
return false;
}
}
}
/*Check that retrieved values match with the give criteri*/
if (typesCount LT typesRequired) {
return false;
}
return true;
}
dump(var=#passwordCheck("Randy33randy!")#);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment