Regular expression for checking password strength

I wrote a regex that could potentially be used to test password strength:

^(?:([AZ])*([az])*(\d)*(\W)*){8,12}$ 

The expression consists of four groups:

  • Zero or lowercase letters
  • Zero or lowercase characters
  • Zero or more decimal digits
  • Zero or more non-word characters (!, £, $,%, etc.)

I want it to work to determine how many groups were matched to determine the strength of the password. for example, if only one group is matched, it will be weak. If all four groups were matched, it would be strong.

I tested the expression using Rubular (Ruby's regular expression editor).

Here I see visually how many groups are mapped, but I want to do this in JavaScript. I wrote a script that returns the number of matched groups, but the results were not what I can see in Rubular.

How can I achieve this in JavaScript? and my regular expression before the task?

+4
source share
6 answers

I think you have to check each group yourself. Pseudo Code:

 bool[] array = {}; array[0] = pwd.match(/[AZ]/); array[1] = pwd.match(/[az]/); array[2] = pwd.match(/\d/); array[3] = pwd.match(/[!_.-]/); int sum = 0; for (int i=0; i<array.length; i++) { sum += array[i] ? 1 : 0; } switch (sum) { case 0: print("weird..."); break; case 1: print("weak"); break; case 2: print("ok"); break; case 3: print("strong"); break; case 4: print("awesome"); break; default: print("weird..."); break; } 
+8
source

Here is my final solution based on sp00m answer:

 function testPassword(pwString) { var strength = 0; strength += /[AZ]+/.test(pwString) ? 1 : 0; strength += /[az]+/.test(pwString) ? 1 : 0; strength += /[0-9]+/.test(pwString) ? 1 : 0; strength += /[\W]+/.test(pwString) ? 1 : 0; switch(strength) { case 3: // its medium! break; case 4: // it strong! break; default: // it weak! break; } } 

I added this solely for reference, however I accepted the sp00m answer as it was their answer that allowed me to find this solution.

+3
source

You can do this by dividing each group and matching them one at a time, for example:

 var level = 0; var input = '';//user input goes here switch(true){ case /^(?:([AZ])*){8,12}$/.test(input): level = 1; break; case /^(?:([AZ])*([az])*){8,12}$/.test(input): level = 2; break; case /^(?:([AZ])*([az])*(\d)*){8,12}$/.test(input): level = 3; break; case /^(?:([AZ])*([az])*(\d)*(\W)*){8,12}$/.test(input): level = 4; break; } 

The level variable goes from 1 (the weakest) to 4 (the strongest).

+1
source

Check out: http://jsfiddle.net/43tu58jf/

 function isSimpleStrongLevel(password){ var stringsOptions = ['123456', '123abc', 'abc123', '654321', '012345', 'password', '123pass', 'pass123', '123456abc']; return stringsOptions.indexOf(password) != -1; } function getStrongLevel(password) { var level = 0; level += password.length > 6 ? 1 : 0; level += /[ !@ #$%^&*?_~]{2,}/.test(password) ? 1 : 0; level += /[az]{2,}/.test(password) ? 1 : 0; level += /[AZ]{2,}/.test(password) ? 1 : 0; level += /[0-9]{2,}/.test(password) ? 1 : 0; return level; } var password = '123456'; var isSimple = isSimpleStrongLevel(password); var level = getStrongLevel(password); console.log(isSimple, level); 
+1
source

my example

Js / jquery

 $( "#password" ).change(function () { strongPassword(); }); /** * @author websky */ function strongPassword() { var p = document.getElementById('password').value; var label1 = 0; var label2 = 0; var label3 = 0; var label4 = 0; var label5 = 0; if (p.length > 6) {//min length label1 = 1; } if (/[ !@ #$%^&*?_~]{2,}/.test(p)) {//min 2 special characters label2 = 1; } if (/[az]{2,}/.test(p)) {//min 2 az label3 = 1; } if (/[AZ]{2,}/.test(p)) {//min 2 AZ label4 = 1; } if (/[0-9]{2,}/.test(p)) {//min 2 0-9 label5 = 1; } var strong_password = label1 + label2 + label3 + label4 + label5; if(strong_password > 0){ //Here your action // //var progressbar = strong_password * 20; //$( "#progressbar" ).progressbar({value: progressbar}); <== I use jQuery progessbar } } 
0
source

Try ==>

 var PasswordStrength = function () { var p_worker = { checkMark: function ( msg, mark ) { let strength = "Required!!!", status = true; switch ( mark ) { case 1: strength = '<b style="color:rgb(200, 200, 200)"> Password strength: Weak...</b>'; break; case 2: strength = '<b style="color:rgb(200, 200, 200)"> Password strength: Semi-weak...</b>'; break; case 3: strength = '<b style="color:green"> Password strength: Medium...</b>'; break; case 4: strength = '<b style="color:green"> Password strength: Strong...</b>'; break; default: status = false; break; } return { status: status/*[is valid or not]*/, cur_strength: strength/**[strength msg]*/, req_msg: msg/**[required msg]*/, mark: mark/**[strength mark]*/ }; }, setting: { n: { rgx: /[0-9]/, msg: '1 Numeric character' }, c: { rgx: /[AZ]/, msg: '1 Alphabet character' }, s: { rgx: /[az]/, msg: '1 Small character' }, sp: { rgx: /[@#$\.%^&+=]/, msg: '1 Special character' }, } }; return { check: function ( value ) { let msg = "", mark = 0, c = 0; for ( let i in p_worker.setting ) { if ( !p_worker.setting[i]['rgx'].test( value ) ) { if ( c === 3 ) { msg += '<d style="color:rgba(219, 177, 177, 0.96)">[*] ' + p_worker.setting[i]['msg'] + '</d>'; c++; continue; } msg += '<d style="color:rgba(219, 177, 177, 0.96)">[*] ' + p_worker.setting[i]['msg'] + ',</d></br>'; c++; continue; } if ( c === 3 ) { msg += '<img src="/image/accept.png" /> <d style="color:green">' + p_worker.setting[i]['msg'] + '</d>'; mark++; c++; continue; } msg += '<img src="/image/accept.png" /> <d style="color:green">' + p_worker.setting[i]['msg'] + ',</d></br>'; mark++; c++; } return p_worker.checkMark( msg, mark ); } } }(); 

Use ==>

 PasswordStrength.check( "1234$#" ); 
0
source

All Articles