Home >Web Front-end >JS Tutorial >Regular match of numeric and alphabetical passwords (detailed steps)
This time I will bring you the regular rules for completely matching numeric and alphabetic passwords (detailed steps), and what are the precautions to achieve complete matching of numeric and alphabetic password rules. The following is a practical case, let’s take a look one time.
The password for a User Registration function has the following requirements: it must be composed of numbers and letters, and must contain both numbers and letters, and the length must be between 8-16 characters.
How to analyze requirements? Split! This is the general idea of software design. So, the splitting requirements are as follows:
1, it cannot be all numbers
2, it cannot be all letters
3, it must be numbers or letters
As long as the above three requirements can be met at the same time, write it out as follows:
^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$
Comment it separately:
^ Match the beginning position of a line
(?! [0-9]+$) Predict that the position is not followed by all numbers
(?![a-zA-Z]+$) Predict that the position is followed by not all letters
[0- 9A-Za-z] {8,16} consists of 8-16 digits or letters
$ Matches the end of the line position
Note: (?!xxxx) isRegular expression is a form of negative zero-width assertion, indicating that the character after the preset position is not xxxx.
The test examples are as follows:
public class Test { public static void main(String[] args) throws Exception { String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$"; String value = "aaa"; // 长度不够 System.out.println(value.matches(regex)); value = "1111aaaa1111aaaaa"; // 太长 System.out.println(value.matches(regex)); value = "111111111"; // 纯数字 System.out.println(value.matches(regex)); value = "aaaaaaaaa"; // 纯字母 System.out.println(value.matches(regex)); value = "####@@@@#"; // 特殊字符 System.out.println(value.matches(regex)); value = "1111aaaa"; // 数字字母组合 System.out.println(value.matches(regex)); value = "aaaa1111"; // 数字字母组合 System.out.println(value.matches(regex)); value = "aa1111aa"; // 数字字母组合 System.out.println(value.matches(regex)); value = "11aaaa11"; // 数字字母组合 System.out.println(value.matches(regex)); value = "aa11aa11"; // 数字字母组合 System.out.println(value.matches(regex)); } }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the 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 Regular match of numeric and alphabetical passwords (detailed steps). For more information, please follow other related articles on the PHP Chinese website!