Jitendra Pancholi Jitendra Pancholi - 6 months ago 5
Javascript Question

Regular expression to validate a string for at least n number of Upper case, Lower case , number and special characters in a given string

I have a requirement to add ability to provide minimum characters (range 0 - 9) for each type (Upper case, Lower case , number and special) characters in password.

enter image description here

I found so many solutions which gives solution for at-least 1 special/uppercase/lowercase/number (Regex for Password: "Atleast 1 letter, 1 number, 1 special character and SHOULD NOT start with a special character") but there is no generic solution which can satisfy my requirement.

I tried below for at least n special characters in my string but it's not working.

function CheckSpecialChars(n, NewPassword){
var PasswordPattern ="^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+]{n})[A-Za-z\\d!@#$%^&*()_+]{8,20}$";
var NewPassword = $('#txt').val();
var PasswordRegEx = new RegExp(PasswordPattern, 'g');
if (!PasswordRegEx.test(NewPassword)) {
$('.er').html('not matched');
return false;
}else{
$('.er').html('matched');
return false;
}
}

// if minimum 2 special characters are mandatory
Valid String: sad@j234KSS&ff // has more than 2 special chars
Invalid String: sdf#kj034950 // has less than 2 special chars

Answer

You will need to construct a regular expression using the constructor since n is variable. Here is an example:

Constructed regex with n = 2:

var n = 2;
var constructedRegEx = "^(?=(?:.*[0-9]){" + n + ",})(?=(?:.*[a-z]){" + n + ",})(?=(?:.*[A-Z]){" + n + ",})(?=(?:.*[[!@#$%^&*()_+]){" + n + ",}).+$";

var PasswordRegEx = new RegExp(constructedRegEx, 'm');

console.log(PasswordRegEx.test('@Al1#a2B'));
console.log(PasswordRegEx.test('@Al1#a2'));

Example of a constructed regex:

^(?=(?:.*[0-9]){2,})(?=(?:.*[a-z]){2,})(?=(?:.*[A-Z]){2,})(?=(?:.*[!@#$%^&*]){2,}).+$

Railroad Diagram:

Regular expression visualization

Description:

    NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [!@#0^&*]                any character of: '!', '@', '#', '0',
                               '^', '&', '*'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

Regex101

Comments