StrengthView.js 3.3 KB

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