HTML5 Pattern Regex Password Match

Look for some help with password verification with the following rules:

8+ characters

contains at least 1 uppercase letter

contains at least 1 lowercase letter

contains at least 1 number

Unable to start from number

no special characters

I got to:

(?=.*\d.*)(?=.*[az].*)(?=.*[AZ].*)(?=.*[!#\$%&\?].*).{8,}

but it seems that she cannot figure out how to get the first digit that does not match the digit, and set up a special character class so that it does not match. Any help would be greatly appreciated.

+4
source share
3 answers

Here is what I would like to do with:

 (?=.*\d)(?=.*[az])(?=.*[AZ])(?!.*[!#\$%&\?])^\D.{7} 

Please note that .* After each waiting period was redundant.

(?!...) is a negative outlook to make sure there are no special characters.

^\D requires the first character to be unsigned. Then I just require 7 characters after that, because the end is not executed.

But why exclude special characters from passwords? The opposite is usually recommended.

+3
source

I believe that breaking this down into separate tests:

  • easier for code
  • easier to read
  • easier to maintain
  • and more flexible when changing requirements

Try something like this:

 var testPassword = function (password) { var minLengthMet = password.length >= 8, hasUpper = (/[AZ]+/).test(password), hasLower = (/[az]+/).test(password), hasNumber = (/[0-9]+/).test(password), letterBegin = (/^[A-Za-z]/).test(password), noSpecials = !(/[^A-Za-z0-9]+/).test(password); return minLengthMet && hasUpper && hasLower && hasNumber && letterBegin && noSpecials; }; 

See here: http://jsfiddle.net/H9twa/

+4
source

What about:

 pwd.length >= 8 && pwd.match(/[AZ]/) && pwd.match(/[az]/) && pwd.match(/\d/) && !pwd.match(/^\d/) && !pwd.match(/[!#\$%&\?]/); 

Just in case, do you need to maintain this code ever?

+3
source

Source: https://habr.com/ru/post/1485040/


All Articles