PHP Classes

File: assets/js/ahpwcheck.class.min.js

Recommend this page to a friend!
  Classes of zinsou A.A.E.Moïse   PHP Mnemonic Secret Path Login   assets/js/ahpwcheck.class.min.js   Download  
File: assets/js/ahpwcheck.class.min.js
Role: Auxiliary script
Content type: text/plain
Description: javascript class source to check passwords requirements
Class: PHP Mnemonic Secret Path Login
Detect human users telling to click on page places
Author: By
Last change:
Date: 6 years ago
Size: 5,180 bytes
 

Contents

Class file image Download
var ahpwcheck=function(aOptions){this._oDivOut=false;this._oInput=false;this.name=false;this._sCallback=false;this.aLang={introtext:"Check of the password - matching requirements: [COUNT]",lcase:"lower case: [COUNT]",ucase:"upper case: [COUNT]",digits:"digits: [COUNT]",special:"special chars: [COUNT]",count:"chars (min [REQUIRED]): [COUNT]"};this.aResult={};this.aChecks={lcase:{required:1,weight:1},ucase:{required:1,weight:1},digits:{required:1,weight:1.5},special:{required:1,weight:2},count:{required:8,weight:1}};this.watch=function(sIdInput,sIdOut){this._getName();this._oInput=document.getElementById(sIdInput);if(this._oInput){eval(" document.getElementById('"+sIdInput+"').addEventListener('change', function () {"+this.name+".checkPw()}, false);\n document.getElementById('"+sIdInput+"').addEventListener('keydown', function () {"+this.name+".checkPw()}, false);\n document.getElementById('"+sIdInput+"').addEventListener('keyup', function () {"+this.name+".checkPw()}, false);\n ")}this._oDivOut=document.getElementById(sIdOut);this.renderAll();return true};this._getName=function(){for(var name in this.global){if(this.global[name]===this){return this._setName(name)}}};this._setName=function(sName){this.name=sName;return this.name};this.setOptions=function(aOptions){console.log(aOptions);if(aOptions){for(var s in aOptions){switch(s){case"callback":this._sCallback=aOptions[s];break;case"checks":for(var s2 in this.aChecks){if(aOptions[s][s2]){if("required" in aOptions[s][s2]){this.aChecks[s2]["required"]=aOptions[s][s2]["required"]}if("weight" in aOptions[s][s2]){this.aChecks[s2]["weight"]=aOptions[s][s2]["weight"]}}}break;case"lang":for(var s2 in this.aLang){if(aOptions[s][s2]){this.aLang[s2]=aOptions[s][s2]}}break}}}return true};this._calcScore=function(){var iChecks=0;var iTotalMax=0;var strength=0;for(var s in this.aChecks){iRequired=this.aChecks[s]["required"];if(iRequired>0){fWeight=this.aChecks[s]["weight"];iChecks++;iTotalMax+=iRequired*fWeight}}for(var s in this.aChecks){iRequired=this.aChecks[s]["required"];if(iRequired){iVal=this.aChecks[s]["count"];fWeight=this.aChecks[s]["weight"];if(!iVal){iVal=0}if(iVal>iRequired){iVal=iRequired}strength+=iVal*fWeight}}return Math.round(strength/iTotalMax*1000)/1000};this._countCapitalLetters=function(s){return s.replace(/[^A-Z]*/g,"").length};this._countDigits=function(s){return s.replace(/[^0-9]*/g,"").length};this._countLowerLetters=function(s){return s.replace(/[^a-z]*/g,"").length};this._countSpecialchars=function(s){return s.replace(/[a-zA-Z0-9]*/g,"").length};this._resetChecks=function(){var iChecks=0;for(var s in this.aChecks){if(this.aChecks[s]["required"]){iChecks++}this.aChecks[s]["count"]=0;this.aChecks[s]["check"]=false;this.aChecks[s]["label"]=this.aLang[s]}this.aResult.count=0;this.aResult.required=iChecks;this.aResult.check=false;this.aResult.score=false;this.aResult.label=this.aLang.introtext};this._passTests=function(sPassword){this._resetChecks();if(sPassword){this.aChecks.lcase["count"]=this._countLowerLetters(sPassword);this.aChecks.ucase["count"]=this._countCapitalLetters(sPassword);this.aChecks.digits["count"]=this._countDigits(sPassword);this.aChecks.special["count"]=this._countSpecialchars(sPassword);this.aChecks.count["count"]=sPassword.length}var iOK=0;var iChecks=0;for(var s in this.aChecks){this.aChecks[s]["check"]=this.aChecks[s]["count"]>=this.aChecks[s]["required"];iChecks++;if(this.aChecks[s]["check"]){iOK++}}this.aResult.count=iOK;this.aResult.check=iOK>this.aResult.required;this.aResult.score=this._calcScore()};this.getChecks=function(){var aReturn={};for(var s in this.aChecks){aReturn[s]={required:this.aChecks[s]["required"],weight:this.aChecks[s]["weight"]}}return aReturn};this.getResult=function(sPassword){this._passTests(sPassword);var aReturn=this.aResult;delete aReturn.label;return aReturn};this.getScore=function(sPassword){this._passTests(sPassword);return this.aResult.score};this._ReplaceInt=function(sLabel,sReplace,iValue){if(!iValue){iValue=0}return sLabel.replace(sReplace,iValue)};this._getLabel=function(aItem){var sLabel=aItem.label;sLabel=this._ReplaceInt(sLabel,"[COUNT]",aItem.count);sLabel=this._ReplaceInt(sLabel,"[REQUIRED]",aItem.required);if("score" in aItem){sLabel=this._ReplaceInt(sLabel,"[SCORE]",aItem.score)}return sLabel};this.checkPw=function(){var sPassword=this._oInput.value;this._passTests(sPassword);this.renderAll();if(this._sCallback){eval(this._sCallback)}};this.renderAll=function(){var sHtml="";sHtml+='<div class="divahpwcheck">';sHtml+=this.renderIntro();sHtml+=this.renderChecks();sHtml+='<div style="clear: both;"></div>';sHtml+="</div>";this._oDivOut.innerHTML=sHtml};this.renderChecks=function(){var sHtml="";for(var s in this.aChecks){if(this.aChecks[s]["required"]>0){this.aChecks[s]["label"]=this.aLang[s];sHtml+='<div class="divCheck'+(this.aChecks[s]["check"]?" checkok":"")+'" id="'+s+'">'+this._getLabel(this.aChecks[s])+"</div>"}}return sHtml};this.renderIntro=function(){return"<p>"+this._getLabel(this.aResult)+"</p>"};if(aOptions){this.setOptions(aOptions)}this._resetChecks()};ahpwcheck.prototype.global=this;