You are viewing limited content. For full access, please sign in.

Question

Question

Password Pattern Matching aka Regular Expresion

asked on November 5, 2015

Does anyone know how the Regular Expression would look like for a password field with these requirements (or similar to these)

 

Has to contain 3 out of 4:

Capital letter

Lower case letter

Number

Special Character

Has to be longer than 8 characters

 

Thank you!!

0 0

Answer

SELECTED ANSWER
replied on November 5, 2015

One option is to first check the length and then check the complexity requirements and make sure that at least three out of four are used. Try the JavaScript below. The password field on the form will need the CSS class name, pwd.

$(document).ready(function () {

  $('.pwd input').on('blur', function() {
    var minMaxLength = /^[\s\S]{8,32}$/;
    var upper = /[A-Z]/;
    var lower = /[a-z]/;
    var number = /[0-9]/;
    var special = /[^A-Za-z0-9]/;
    var count = 0;
    
    $('p.complexityWarning').remove();
    $('p.lengthWarning').remove();
    
    if (minMaxLength.test($(this).val())) {
      $('p.lengthWarning').remove();
      if (upper.test($(this).val())) count++;
      if (lower.test($(this).val())) count++;
      if (number.test($(this).val())) count++;
      if (special.test($(this).val())) count++;
    
      if (count < 3) {
        $('<p class="complexityWarning">The password does not meet the complexity requirements.</p>').insertAfter('.pwd');
        $('.Submit').attr('disabled', true);
      } else {
        $('p.complexityWarning').remove();
        $('.Submit').removeAttr('disabled');
      }
    } else {
      if (upper.test($(this).val())) count++;
      if (lower.test($(this).val())) count++;
      if (number.test($(this).val())) count++;
      if (special.test($(this).val())) count++;
    
      if (count < 3) {
        $('<p class="complexityWarning">The password does not meet the complexity requirements.</p>').insertAfter('.pwd');
      } else {
        $('p.complexityWarning').remove();
      }
      $('<p class="lengthWarning">The password is not between 8 and 32 characters.</p>').insertAfter('.pwd');
      $('.Submit').attr('disabled', true);
    }
  });
  
});

1 0
replied on November 3, 2017

Alexander, is there a way to add a few more things to this code - don't accept "password" or "Password", and don't accept the person's name (based on a variable)?

0 0

Replies

replied on November 5, 2015

Lidija,

Here is what I use: 

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@.,><*#$%]).{8,20})

Add/remove any special characters from the [ ] and change the length to be whatever you want in {#, #}

Hope this helps.  

4 0
replied on November 5, 2015 Show version history

I believe this pattern requires all 4 character classes.  There's no simple way to do "3 out of 4", you have to combine those assertions at the beginning in groups of 3, something like this (which is untested):

(((?=.*\d)(?=.*[a-z])(?=.*[A-Z]) | (?=.*\d)(?=.*[a-z])(?=.*[@.,><*#$%]) | (?=.*\d)(?=.*[A-Z])(?=.*[@.,><*#$%]) | (?=.*[a-z])(?=.*[A-Z])(?=.*[@.,><*#$%])).{8,20})

3 0
replied on November 9, 2015

Thank you all for your wonderful help but I will make Alexander's post as the answer since it does exactly what I need.

 

Thanks again!!!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.