效果:
新建一个密码强度检测工具文件 password-strength.js :
// has number const hasNumber = (number) => new RegExp(/[0-9]/).test(number); // has mix of small and capitals const hasMixed = (number) => new RegExp(/[a-z]/).test(number) && new RegExp(/[A-Z]/).test(number); // has special chars const hasSpecial = (number) => new RegExp(/[!#@$%^&*)(+=._-]/).test(number); // set color based on password strength export const strengthColor = (count) => { if (count < 2) return { lever: count, label: 'Poor', color: '#C0C0C0' }; if (count < 3) return { lever: count, label: 'Weak', color: '#969696' }; if (count < 4) return { lever: count, label: 'Normal', color: '#808080' }; if (count < 5) return { lever: count, label: 'Good', color: '#646464' }; if (count < 6) return { lever: count, label: 'Strong', color: '#4B4B4B' }; return { lever: count,label: 'Poor', color: '#C0C0C0' }; }; // password strength indicator export const strengthIndicator = (number) => { let strengths = 0; if (number.length > 5) strengths += 1; if (number.length > 7) strengths += 1; if (hasNumber(number)) strengths += 1; if (hasSpecial(number)) strengths += 1; if (hasMixed(number)) strengths += 1; return strengthColor(strengths); };
如何使用:
import {strengthIndicator} from "@/utils/password-strength"; strengthIndicator(1234565)
标签:count,return,color,number,js,密码,label,strengths,检测工具 From: https://www.cnblogs.com/zy-mg/p/16857279.html