Home >Web Front-end >JS Tutorial >Use regular expression verification to determine the strength of the password and prompt it

Use regular expression verification to determine the strength of the password and prompt it

php中世界最好的语言
php中世界最好的语言Original
2018-03-29 15:08:571902browse

This time I will bring you the regular expression verification to determine the strength of the password and prompts, and the use of regular expression verification to determine the strength of the password and promptNotes What are they? The following is a practical case. Let’s take a look.

I learned python’s re template and wrote an article but no one read it, so I summarized my experience. No one loves theory, but people’s hearts are in practice. So since no one likes theory, just go straight to practice and refine in practice. theory. Without further ado, let’s just start with the code

def password_level(password):
 weak = re.compile(r'^((\d+)|([A-Za-z]+)|(\W+))$')
 level_weak = weak.match(password)
 level_middle = re.match(r'([0-9]+(\W+|\_+|[A-Za-z]+))+|([A-Za-z]+(\W+|\_+|\d+))+|((\W+|\_+)+(\d+|\w+))+',password)
 level_strong = re.match(r'(\w+|\W+)+',password)
 if level_weak:
  print 'password level is weak',level_weak.group()
 else:
  if (level_middle and len(level_middle.group())==len(password)):
   print 'password level is middle',level_middle.group()
  else:
   if level_strong and len(level_strong.group())==len(password):
    print 'password level is strong',level_strong.group()

Explain it

Weak password: all numbers, symbols, letters

Medium password: numbers plus symbols, numbers plus letters , letters plus symbols

Strong password: a mix of three.

I am not case sensitive, I hope those who are interested can write it themselves. The problem occurs with \w because \w is equivalent to [A-Za-z0-9_], so the string containing the underline cannot be matched through \W in the early stage

Let’s take a look Look at the medium password. Numbers plus symbols or letters or _ are a group. Letters plus symbols or underscores or symbols are a group. Symbols or underscores plus letters or numbers are a group. I always feel that the code in this seems wrong, but After passing the test and finding nothing wrong, just use this version 0.0.1 first

test code

if name == 'main':
 passwords = ('11','aa','LL','1a','1_','a_','a1','_1','*a','1a_','1a<')
 for pw in passwords:
  password_level(pw)
'''----------------------output------------------------
#password level is weak 11
#password level is weak aa
#password level is weak LL
#password level is middle 1a
#password level is middle 1_
#password level is middle a_
#password level is middle a1
#password level is middle _1
#password level is middle *a
#password level is strong 1a_
#password level is strong 1a<
'''

I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website!

Recommended reading:

How to use regular expressions in js (with code)

Use regular expressions to verify users Account password, mobile phone number and ID number

The above is the detailed content of Use regular expression verification to determine the strength of the password and prompt it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn