password.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // 导入配置
  2. import config from '@/uni_modules/uni-id-pages/config.js'
  3. const {passwordStrength} = config
  4. // 密码强度表达式
  5. const passwordRules = {
  6. // 密码必须包含大小写字母、数字和特殊符号
  7. super: /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
  8. // 密码必须包含字母、数字和特殊符号
  9. strong: /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
  10. // 密码必须为字母、数字和特殊符号任意两种的组合
  11. medium: /^(?![0-9]+$)(?![a-zA-Z]+$)(?![~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]+$)[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
  12. // 密码必须包含字母和数字
  13. weak: /^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{6,16}$/
  14. }
  15. const ERROR = {
  16. normal: {
  17. noPwd: '请输入密码',
  18. noRePwd: '再次输入密码',
  19. rePwdErr: '两次输入密码不一致'
  20. },
  21. passwordStrengthError: {
  22. super: '密码必须包含大小写字母、数字和特殊符号,密码长度必须在8-16位之间',
  23. strong: '密码必须包含字母、数字和特殊符号,密码长度必须在8-16位之间',
  24. medium: '密码必须为字母、数字和特殊符号任意两种的组合,密码长度必须在8-16位之间',
  25. weak: '密码必须包含字母,密码长度必须在6-16位之间'
  26. }
  27. }
  28. function validPwd(password) {
  29. //强度校验
  30. if (passwordStrength && passwordRules[passwordStrength]) {
  31. if (!new RegExp(passwordRules[passwordStrength]).test(password)) {
  32. return ERROR.passwordStrengthError[passwordStrength]
  33. }
  34. }
  35. return true
  36. }
  37. function getPwdRules(pwdName = 'password', rePwdName = 'password2') {
  38. const rules = {}
  39. rules[pwdName] = {
  40. rules: [{
  41. required: true,
  42. errorMessage: ERROR.normal.noPwd,
  43. },
  44. {
  45. validateFunction: function(rule, value, data, callback) {
  46. const checkRes = validPwd(value)
  47. if (checkRes !== true) {
  48. callback(checkRes)
  49. }
  50. return true
  51. }
  52. }
  53. ]
  54. }
  55. if (rePwdName) {
  56. rules[rePwdName] = {
  57. rules: [{
  58. required: true,
  59. errorMessage: ERROR.normal.noRePwd,
  60. },
  61. {
  62. validateFunction: function(rule, value, data, callback) {
  63. if (value != data.password) {
  64. callback(ERROR.normal.rePwdErr)
  65. }
  66. return true
  67. }
  68. }
  69. ]
  70. }
  71. }
  72. return rules
  73. }
  74. export default {
  75. ERROR,
  76. validPwd,
  77. getPwdRules
  78. }