The following is a summary of the two schemes of Regular Expressions for password strength in Javascript.

Source: Internet
Author: User
Tags character classes account security

The following is a summary of the two schemes of Regular Expressions for password strength in Javascript.

This article provides two regular expression schemes for password strength, which are simple, more complex, and more secure. The analysis and testing procedures of the two solutions are given respectively. Generally, you can customize your own password regular conventions based on your project's actual needs.

 

Preface

Regular password verification is used during user registration. To write a correct regular expression, you must first define the expression rules.

Solution 1 (simple)

Assume that the password verification rules are defined as follows:

  • Minimum 6 bits, maximum 16 bits {6, 16}
  • Can contain lowercase big mother [a-z] And uppercase letters [A-Z]
  • It can contain numbers [0-9]
  • It can contain underscores (_) and minus signs (-).

According to the above rules, it is easy to define the regular expression literal as follows:

var pattern = /^[\w_-]{6,16}$/;

Solution 1 Analysis

Literal //

The literal of a regular expression is defined as a character that contains a slash (/). For example:

var pattern = /s$/;

The preceding strings match all strings ending with the letter "s.

Character class []

Put the characters in square brackets to form a character class. A character class can match any character it contains. Therefore, the regular expression/[abc]/matches any one of the letters "a", "B", and "c.

Character classes can use hyphens to represent character ranges. To match lowercase Latin letters, use/[a-z]/.

Character class \ w

The character class \ w matches any word consisting of ASCII characters, equivalent to [a-zA-Z0-9].

[\ W _-] indicates matching any uppercase/lowercase Latin letters, followed by an underscore and a minus sign.

Repeated {}

In the regular expression, {} is used to represent the number of repeated occurrences of elements.

  • {N, m} matches the first item at least n times, but cannot exceed m times
  • {N,} matches the previous item n times or more times
  • {N} matches the previous item n times

[\ W _-] {6, 16} indicates matching any Latin and upper-case letters. The number must be followed by an underline or minus sign for at least 6 times, and a maximum of 16 times.

Matching position

^ Match the start of a string. In multi-row search, match the beginning of a line
$ Match the end of a string. In multi-row search, match the end of a row.
/^ \ W/match strings starting with uppercase/lowercase letters or numbers.

Solution 1 Testing

The test results are as follows:

var pattern = /^[\w_-]{6,16}$/;pattern.test('123456') = true;pattern.test('-ifat33') = true;pattern.test('42du') = false;pattern.test('du42du42du42du421') = false;pattern.test('42du42@') = false;

View Source Code

According to the test results, we can see that solution 1 only limits the password and does not guarantee the password strength and account security.

Solution 2 (Security)

Assume that the password verification rules are defined as follows:

  • Minimum 6 bits, maximum 16 bits {6, 16}
  • Must contain 1 digit
  • Must contain 2 lower-case letters
  • Must contain 2 uppercase letters
  • Must contain 1 Special Character

According to the above rules, it is easy to define the regular expression literal as follows:

var pattern = /^.*(?=.{6,16})(?=.*\d)(?=.*[A-Z]{2,})(?=.*[a-z]{2,})(?=.*[!@#$%^&*?\(\)]).*$/;

Solution 2 Analysis

Character class.

Character class. Any character except the line break and other Unicode line terminator.

Forward first assertion (? =)

In the symbol "(? = "And") "are added to an expression, which is a predicate to indicate that the expressions in parentheses must match correctly. For example:/Java (? = \:)/It can only match Java with a colon.

(?=.*[!@#$%^&*?\(\)])

This predicate indicates that it must contain a special character. The 10 special characters in the above expression are the top key characters of keyboard 1, 2... 0. You can also add other special characters. Note: If you add a character that has special meanings in a regular expression, you must add a backslash (\) escape before the symbol.

Solution 2 Test

The test results are as follows:

var pattern = /^.*(?=.{6,16})(?=.*\d)(?=.*[A-Z]{2,})(?=.*[a-z]{2,})(?=.*[!@#$%^&*?\(\)]).*$/;pattern.test('du42DU!') = true;pattern.test('duDUd!') = false;pattern.test('42dud!') = false;pattern.test('42DUD!') = false;pattern.test('42duDU') = false;pattern.test('42duU(') = false;pattern.test('42dUU!') = false;

Summary

The above is a summary of the two schemes JS, namely the regular expression of the password strength, introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.