Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 检查密码是否符合规则,如果符合规则返回true
- * @param {string} pwd
- * @return {boolean}
- */
- function check(pwd) {
- return (
- notSeries(pwd, "1234567890", 3) &&
- notSeries(pwd, "abcdefghijklmnopqrstuvwxyz", 3) &&
- notSeries(pwd, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3) &&
- notSeries(pwd, "qwertyuiop", 3) &&
- notSeries(pwd, "QWERTYUIOP", 3) &&
- notSeries(pwd, "asdfghjkl", 3) &&
- notSeries(pwd, "ASDFGHJKL", 3) &&
- notSeries(pwd, "zxcvbnm", 3) &&
- notSeries(pwd, "ZXCVBNM", 3) &&
- notSeries(pwd, "~!@#$%^&*()_+", 3) &&
- notRepeat(pwd, 3)
- );
- }
- /**
- * 获取字符串的字符数组
- * @param {string} str
- * @return {array}
- */
- function getChars(str) {
- return str.split("").map(c => c.charCodeAt(0));
- }
- /**
- * 判断是否包含连续字符串,如果不包含返回true
- * @param {string} pwd
- * @param {string} str
- * @param {number} n
- * @return {boolean}
- */
- function notSeries(pwd, str, n) {
- const c1 = getChars(pwd);
- const c2 = getChars(str);
- for (let i = 0; i < c1.length - n + 1; i++) {
- const j = c2.indexOf(c1[i]);
- if (j !== -1 && j < str.length - n) {
- let ok = true;
- for (let k = 1; k < n; k++) {
- ok = ok && c1[i + k] === c2[j + k];
- }
- if (ok) return false;
- }
- }
- return true;
- }
- /**
- * 判断是否包含重复的字符串,如果不包含返回true
- * @param {string} pwd
- * @param {number} n
- */
- function notRepeat(pwd, n) {
- const c = getChars(pwd);
- for (let i = 0; i < c.length - n + 1; i++) {
- let ok = true;
- for (let j = 1; j < n; j++) {
- ok = ok && c[i] === c[i + j];
- }
- if (ok) return false;
- }
- return true;
- }
- /////////////////////////////////////////////////////////////
- // 测试
- const assert = require("assert");
- assert.ok(check("hello, world"));
- assert.ok(check("rhu2ihfjsn"));
- assert.ok(check("112"));
- assert.ok(check("233"));
- assert.ok(!check("111"));
- assert.ok(!check("safqwert"));
- assert.ok(!check("qwasd!@#"));
- console.log('测试通过,666');
Add Comment
Please, Sign In to add comment