Home Web Front-end JS Tutorial Verify password strength regex set by user

Verify password strength regex set by user

Mar 29, 2018 am 10:29 AM
password strength set up

This time I will bring you the password strength for verifying the password set by the user. Regular expression. What are the precautions for verifying the regular expression for the password strength set by the user. The following is a practical case. Let’s take a look.

This article gives two regular expression schemes for password strength, one simple and one more complex and secure. And the analysis and testing procedures of the two solutions are given respectively. Generally, you can define your own password regular conventions based on the actual needs of your project.

Preface

##When users register, password regularization will be used test. To write correct regular expressions, you must first define expression rules.

Option 1 (Simple)

Assume that password verification is defined as follows:

  • Shortest 6 digits, up to 16 digits {6,16}

  • can include lowercase letters [a-z] and uppercase letters [A-Z]

  • Can contain numbers [0-9]

  • Can contain underscores [_] and minus signs [-]

According to the above rules, it is easy The regular literal definition is given as follows:

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

Scheme 1 Analysis

Literal/ /

regular expression A literal is defined as a character contained between a pair of slashes (/), for example:

var pattern = /s$/;
Copy after login
The above literal matches all strings ending with the letter "s".

Character class [ ]

Put 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 of the letters "a", "b", or "c".

Character classes may use hyphens to represent

character ranges . To match Latin lowercase letters use /[a-z]/ .

Character class \w

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

[\w_-] means matching any Latin uppercase and lowercase letters, numbers plus underscores and minus signs.

Repeat {}

Use { } in regular expressions to represent the number of times an element repeats.

  • {n,m} Match the previous item at least n times, but not more than m times

  • {n,} Match the previous item n times or more

  • {n} matches the previous item n times

[\w_-]{6,16} means matching any Latin uppercase and lowercase letters, numbers plus underscores and minus signs appearing at least 6 times and at most 16 times.

Matching position

^ Matches the beginning of the string, in multi-line retrieval, matches the beginning of a line

$ Matches the end of the string, in multiple lines During retrieval, match the end of a line
/^\w/ and match strings starting with uppercase and lowercase letters or numbers.

Option 1 test

The test results are given 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;
Copy after login
View source code

According to the test It can be seen from the results that Solution 1 only briefly limits the password and cannot guarantee the strength of the password and the security of the account.

Option 2 (Security)

Assume that password verification is defined as follows:

  • Shortest 6 digits, up to 16 digits {6,16}

  • must contain 1 number

  • must contain 2 lowercase letters

  • must contain 2 uppercase letters

  • must contain 1

    special character

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

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

方案2分析

字符类 .

字符类 . 表示除换行符和其他Unicode行终止符之外的任意字符。

正向先行断言 (?= )

在符号“(?=” 和 “)” 之间加入一个表达式,它就是一个先行断言,用以说明圆括号内的表达式必须正确匹配。比如: /Java(?=\:)/ 只能匹配Java且后面有冒号的。

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

该先行断言表示,必须包括一个特殊字符。上述表达式中的10个特殊字符为键盘1,2...0的上档键字符,也可以添加别的特殊字符。注意:如果添加字符是正则表达式中具有特殊含义的,需要在符号前加反斜线(\)转义。

方案2测试

给出测试结果如下:

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;
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Vue.js自定义事件如何进行表单输入组件

Vue用下表修改数组时页面不渲染如何处理

The above is the detailed content of Verify password strength regex set by user. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? Apr 11, 2024 am 09:10 AM

When using Win11 system, sometimes you will encounter a prompt that requires you to enter the administrator username and password. This article will discuss how to deal with this situation. Method 1: 1. Click [Windows Logo], then press [Shift+Restart] to enter safe mode; or enter safe mode this way: click the Start menu and select Settings. Select "Update and Security"; select "Restart Now" in "Recovery"; after restarting and entering the options, select - Troubleshoot - Advanced Options - Startup Settings -&mdash

How to set the scheduled time for publishing works on Douyin? How does it set the video duration? How to set the scheduled time for publishing works on Douyin? How does it set the video duration? Mar 27, 2024 pm 06:11 PM

Publishing works on Douyin can attract more attention and likes, but sometimes it may be difficult for us to publish works in real time. In this case, we can use Douyin's scheduled release function. Douyin’s scheduled release function allows users to automatically publish works at a scheduled time, which can better plan the release plan and increase the exposure and influence of the work. 1. How to set the scheduled time for publishing works on Douyin? To set a scheduled release time, first go to Douyin's personal homepage, find the "+" button in the upper right corner, and click to enter the release page. There is a clock icon in the lower right corner of the publishing page. Click to enter the scheduled publishing interface. In the interface, you can choose the type of work you want to publish, including short videos, long videos, and live broadcasts. Next, you need to set a time for your work to be published. TikTok provides

How to set router WiFi password using mobile phone (using mobile phone as tool) How to set router WiFi password using mobile phone (using mobile phone as tool) Apr 24, 2024 pm 06:04 PM

Wireless networks have become an indispensable part of people's lives in today's digital world. Protecting the security of personal wireless networks is particularly important, however. Setting a strong password is key to ensuring that your WiFi network cannot be hacked by others. To ensure your network security, this article will introduce in detail how to use your mobile phone to change the router WiFi password. 1. Open the router management page - Open the router management page in the mobile browser and enter the router's default IP address. 2. Enter the administrator username and password - To gain access, enter the correct administrator username and password in the login page. 3. Navigate to the wireless settings page - find and click to enter the wireless settings page, in the router management page. 4. Find the current Wi

Incorrect password, beware BitLocker warning Incorrect password, beware BitLocker warning Mar 26, 2024 am 09:41 AM

This article will explore how to solve the problem of wrong password, especially the need to be careful when dealing with BitLocker warnings. This warning is triggered when an incorrect password is entered multiple times in BitLocker to unlock the drive. Usually, this warning occurs because the system has a policy that limits incorrect login attempts (usually three login attempts are allowed). In this case, the user will receive an appropriate warning message. The complete warning message is as follows: The password entered is incorrect. Please note that continuously entering incorrect passwords will cause the account to be locked. This is to protect the security of your data. If you need to unlock your account, you will need to use a BitLocker recovery key. The password is incorrect, beware the BitLocker warning you receive when you log in to your computer

Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately? Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately? Mar 27, 2024 am 11:01 AM

As one of the most popular short video platforms in the world, Douyin allows everyone to become a creator and share every moment of life. For Douyin users, tags are a very important function. It can help users better classify and retrieve content, and also allows the platform to push appropriate content to users more accurately. So, where are the Douyin tags set? This article will explain in detail how to set up and use tags on Douyin. 1. Where is the Douyin tag set? Using tags on Douyin can help users better classify and label their works, making it easier for other users to find and follow them. The method to set the label is as follows: 1. Open the Douyin APP and log in to your account. 2. Click the "+" sign at the bottom of the screen and select the "Publish" button. 3.

Tutorial on changing wifi password on mobile phone (simple operation) Tutorial on changing wifi password on mobile phone (simple operation) Apr 26, 2024 pm 06:25 PM

Wireless networks have become an indispensable part of our lives with the rapid development of the Internet. In order to protect personal information and network security, it is very important to change your wifi password regularly, however. To help you better protect your home network security, this article will introduce you to a detailed tutorial on how to use your mobile phone to change your WiFi password. 1. Understand the importance of WiFi passwords. WiFi passwords are the first line of defense to protect personal information and network security. In the Internet age, understanding its importance can better understand why passwords need to be changed regularly. 2. Confirm that the phone is connected to wifi. First, make sure that the phone is connected to the wifi network whose password you want to change before changing the wifi password. 3. Open the phone’s settings menu and enter the phone’s settings menu.

How to set up scheduled publishing on Weibo_Tutorial on how to set up scheduled publishing on Weibo How to set up scheduled publishing on Weibo_Tutorial on how to set up scheduled publishing on Weibo Mar 29, 2024 pm 03:51 PM

1. Open the Weibo client, click the three little dots on the editing page, and then click Scheduled Post. 2. After clicking on scheduled posting, there will be a time option on the right side of the publishing time. Set the time, edit the article, and click on the yellow words in the lower right corner to schedule posting. 3. The mobile version of Weibo does not currently support scheduled publishing. This function can only be used on the PC client!

What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements? What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements? Jun 25, 2024 pm 04:59 PM

In the Windows 10 system, the password policy is a set of security rules to ensure that the passwords set by users meet certain strength and complexity requirements. If the system prompts that your password does not meet the password policy requirements, it usually means that your password does not meet the requirements set by Microsoft. standards for complexity, length, or character types, so how can this be avoided? Users can directly find the password policy under the local computer policy to perform operations. Let’s take a look below. Solutions that do not comply with password policy specifications: Change the password length: According to the password policy requirements, we can try to increase the length of the password, such as changing the original 6-digit password to 8-digit or longer. Add special characters: Password policies often require special characters such as @, #, $, etc. I

See all articles