StrengthView.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * 密码强度验证组件
  3. * @邠心vbe on 2023/02/01
  4. */
  5. import React from 'react';
  6. import { View, Text, StyleSheet } from 'react-native';
  7. import TextView from '../../components/TextView';
  8. const getStyle = (strength, num) => {
  9. if (strength >= num) {
  10. return {...styles.strengthItem, backgroundColor: colorAccent};
  11. } else {
  12. return styles.strengthItem;
  13. }
  14. }
  15. const applyStrength = (text, version, back) => {
  16. var strength = 0;
  17. if (text.length >= 8) {
  18. strength += 1;
  19. }
  20. if (/\d{1,}/.test(text)) {
  21. strength += 1;
  22. }
  23. if (version == 1 || version == 2) {
  24. if (/[a-z]{1,}/.test(text)) {
  25. strength += 1;
  26. }
  27. if (/[A-Z]{1,}/.test(text)) {
  28. strength += 1;
  29. }
  30. } else {
  31. if (/[A-z]{1,}/.test(text)) {
  32. strength += 1;
  33. }
  34. }
  35. if (version == 1 || version == 3) {
  36. if (/\W{1,}/.test(text)) {
  37. strength += 1;
  38. }
  39. }
  40. back(strength);
  41. }
  42. /**
  43. * 密码强度组件
  44. * @param {*} param0 动态强度
  45. * @returns 组件
  46. */
  47. const StrengthView = ({version, strength}) => (
  48. <View>
  49. <View style={styles.strengthView}>
  50. <TextView style={styles.labelText}>{$t('sign.passwordStrength')}</TextView>
  51. <>
  52. <Text style={getStyle(strength, 1)}></Text>
  53. <Text style={getStyle(strength, 2)}></Text>
  54. <Text style={getStyle(strength, 3)}></Text>
  55. { version < 4 && <>
  56. <Text style={getStyle(strength, 4)}></Text>
  57. { version < 2 &&
  58. <Text style={getStyle(strength, 5)}></Text>
  59. }
  60. </>
  61. }
  62. </>
  63. </View>
  64. <View>
  65. <TextView style={styles.passwordRole}>{$t('sign.passwordMustHave')}</TextView>
  66. <TextView style={styles.passwordRole}>- {$t('sign.password8more')}</TextView>
  67. { (version == 1 || version == 2) &&
  68. <TextView style={styles.passwordRole}>- {$t('sign.passwordUpperLower')}</TextView>
  69. }
  70. { (version == 1 || version == 3)
  71. ? <TextView style={styles.passwordRole}>- {$t('sign.passwordLeastNumberAndSpecial')}</TextView>
  72. : <TextView style={styles.passwordRole}>- {$t('sign.passwordLeastNumber')}</TextView>
  73. }
  74. </View>
  75. </View>
  76. )
  77. export default {
  78. /**
  79. * 最严格的密码强度组件(5)
  80. */
  81. V1: {
  82. VIEW: (props) => <StrengthView version={1} {...props}/>,
  83. maxStrength: 5,
  84. apply: (text, back) => applyStrength(text, 1, back)
  85. },
  86. /**
  87. * 去掉特殊符号限制的密码强度组件(4)
  88. */
  89. V2: {
  90. VIEW: (props) => <StrengthView version={2} {...props}/>,
  91. maxStrength: 4,
  92. apply: (text, back) => applyStrength(text, 2, back)
  93. },
  94. /**
  95. * 去掉大小写字母限制的密码强度组件(4)
  96. */
  97. V3: {
  98. VIEW: (props) => <StrengthView version={3} {...props}/>,
  99. maxStrength: 4,
  100. apply: (text, back) => applyStrength(text, 3, back)
  101. },
  102. /**
  103. * 去掉大小写字母和特殊符号限制的密码强度组件(3)
  104. */
  105. V4: {
  106. VIEW: (props) => <StrengthView version={4} {...props}/>,
  107. maxStrength: 3,
  108. apply: (text, back) => applyStrength(text, 4, back)
  109. },
  110. }
  111. const styles = StyleSheet.create({
  112. strengthView: {
  113. marginTop: -4,
  114. alignItems: 'center',
  115. paddingBottom: 4,
  116. flexDirection: 'row'
  117. },
  118. labelText: {
  119. fontSize:12,
  120. paddingRight: 4,
  121. color: textPrimary
  122. },
  123. passwordRole: {
  124. color: '#999',
  125. fontSize: 10,
  126. lineHeight: 13
  127. },
  128. strengthItem: {
  129. width: 14,
  130. height: 2.5,
  131. marginLeft: 8,
  132. borderRadius: 3,
  133. backgroundColor: '#CCC'
  134. },
  135. })