You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.4 KiB

  1. // has number
  2. const hasNumber = (number) => new RegExp(/[0-9]/).test(number);
  3. // has mix of small and capitals
  4. const hasMixed = (number) => new RegExp(/[a-z]/).test(number) && new RegExp(/[A-Z]/).test(number);
  5. // has special chars
  6. const hasSpecial = (number) => new RegExp(/[!#@$%^&*)(+=._-]/).test(number);
  7. // set color based on password strength
  8. export const strengthColorEng = (count) => {
  9. if (count < 2) return { label: 'Poor', color: 'error.main' };
  10. if (count < 3) return { label: 'Weak', color: 'warning.main' };
  11. if (count < 4) return { label: 'Normal', color: 'warning.dark' };
  12. if (count < 5) return { label: 'Good', color: 'success.main' };
  13. if (count < 6) return { label: 'Strong', color: 'success.dark' };
  14. return { label: 'Poor', color: 'error.main' };
  15. };
  16. export const strengthColorChi = (count) => {
  17. if (count < 3) return { label: '弱', color: 'error.main' };
  18. if (count < 4) return { label: '普通', color: 'warning.main' };
  19. if (count < 5) return { label: '良好', color: 'success.main' };
  20. if (count < 6) return { label: '強', color: 'success.dark' };
  21. return { label: '弱', color: 'error.main' };
  22. };
  23. // password strength indicator
  24. export const strengthIndicator = (number) => {
  25. let strengths = 0;
  26. if (number.length > 5) strengths += 1;
  27. if (number.length > 7) strengths += 1;
  28. if (hasNumber(number)) strengths += 1;
  29. if (hasSpecial(number)) strengths += 1;
  30. if (hasMixed(number)) strengths += 1;
  31. return strengths;
  32. };