This article mainly introduces the complete examples of JS password generation and strength detection, including JavaScript password generation, cracking time calculation, password security monitoring, case lock judgment, and other functions, the demo source code is provided for readers to download and reference. If you need it, you can refer to the examples in this article to describe how to generate and detect JS passwords. We will share this with you for your reference. The details are as follows:
1. generate strong passwords
As follows:
The related code is as follows:
function getPwd(n){ var s = ''; while(n--) s += String.fromCharCode(33 + Math.floor(Math.random()*(126-33))) document.getElementById('txt1').value = s;}
2. Calculate the password cracking time
As follows:
The related code is as follows:
Function getTime () {var str = 'expected cracking time'; var selChar = document. getElementById ('selchar '); var txtPwdLen = document. getElementById ('txtpwdlen'); var num = Math. pow (parseInt (selChar. value), parseInt (txtPwdLen. value); str + = formatTime (num/(1024*1024*1024*2.4*2); document. getElementById ('span2 '). innerHTML = str;} function formatTime (s) {var str = ''; if (s <1) return 'is less than 1 second! '; S = Math. floor (s); if (s> = 1) str = s % 60 + 'second' + str; s = Math. floor (s/60); if (s> = 1) str = s % 60 + 'Min' + str; s = Math. floor (s/60); if (s> = 1) str = s % 24 + 'hour' + str; s = Math. floor (s/24); if (s> = 1) str = s + 'day' + str; return str ;}
3. Password Security Detection
As follows:
The related code is as follows:
Function showPwd () {var p = document. getElementById ('txt2'). value; if (p. length <4) {showError ('password must have at least four digits! '); Return;} var o = checkPwd (p); if (o. isSame) {showError ('password is a repeated character! '); Return ;}for (var I = 0; i1900 & year <2100) {var month = parseInt (p. substr (); if (! IsNaN (month) & month> 0 & month <13) {var day = parseInt (p. substr (6, 2); if (! IsNaN (day) & day> 0 & day <32) {showError ('do not use the date as the password! '); Return ;}} var hasUpper = false; var hasLow = false; var hasNum = false; var hasOther = false; for (var I = 0; I
= 65 & c <= 90) hasUpper = true; else if (c >=97 & c <= 122) hasLow = true; else if (c> = 48 & c <= 57) hasNum = true; else hasOther = true;} var pwdNum = 0; if (hasUpper) pwdNum + = 26; if (hasLow) pwdNum + = 26; if (hasNum) pwdNum + = 10; if (hasOther) pwdNum + = 32; var num = Math. pow (pwdNum, p. length); var str = 'password length: '+ p. length + 'strength: '+ pwdNum +' expected cracking time: '+ formatTime (num/(1024*1024*1024*2.4*2); var span1 = document. getElementById ('span1'); span1.style. color = 'Blue '; span1.innerHTML = str ;}
4. Check whether the keyboard is locked in uppercase (Caps Lock key status)
As follows:
The related code is as follows:
var $lock = false;function checkCapsLock(fn){ document.documentElement.onkeypress = function(e) { var e = e || event; var k = e.keyCode || e.which; var s = e.shiftKey || (k == 16) || false; if(k>=65&&k<=90)$lock=!s; if(k>=97&&k<=122)$lock=s; fn($lock); } document.documentElement.onkeyup = function(e) { var e = e || event; var k = e.keyCode || e.which; if(k==20)$lock = !$lock; fn($lock); }}
Click here to download the complete instance code.