/** * 密码强度验证组件 * @邠心vbe on 2023/02/01 */ import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; import TextView from '../../components/TextView'; const getStyle = (strength, num) => { if (strength >= num) { return {...styles.strengthItem, backgroundColor: colorAccent}; } else { return styles.strengthItem; } } const applyStrength = (text, version, back) => { var strength = 0; if (text.length >= 8) { strength += 1; } if (/\d{1,}/.test(text)) { strength += 1; } if (version == 1 || version == 2) { if (/[a-z]{1,}/.test(text)) { strength += 1; } if (/[A-Z]{1,}/.test(text)) { strength += 1; } } else { if (/[A-z]{1,}/.test(text)) { strength += 1; } } if (version == 1 || version == 3) { if (/\W{1,}/.test(text)) { strength += 1; } } back(strength); } /** * 密码强度组件 * @param {*} param0 动态强度 * @returns 组件 */ const StrengthView = ({version, strength}) => ( {$t('sign.passwordStrength')} <> { version < 4 && <> { version < 2 && } } {$t('sign.passwordMustHave')} - {$t('sign.password8more')} { (version == 1 || version == 2) && - {$t('sign.passwordUpperLower')} } { (version == 1 || version == 3) ? - {$t('sign.passwordLeastNumberAndSpecial')} : - {$t('sign.passwordLeastNumber')} } ) export default { /** * 最严格的密码强度组件(5) */ V1: { VIEW: (props) => , maxStrength: 5, apply: (text, back) => applyStrength(text, 1, back) }, /** * 去掉特殊符号限制的密码强度组件(4) */ V2: { VIEW: (props) => , maxStrength: 4, apply: (text, back) => applyStrength(text, 2, back) }, /** * 去掉大小写字母限制的密码强度组件(4) */ V3: { VIEW: (props) => , maxStrength: 4, apply: (text, back) => applyStrength(text, 3, back) }, /** * 去掉大小写字母和特殊符号限制的密码强度组件(3) */ V4: { VIEW: (props) => , maxStrength: 3, apply: (text, back) => applyStrength(text, 4, back) }, } const styles = StyleSheet.create({ strengthView: { marginTop: -4, alignItems: 'center', paddingBottom: 4, flexDirection: 'row' }, labelText: { fontSize:12, paddingRight: 4, color: textPrimary }, passwordRole: { color: '#999', fontSize: 10, lineHeight: 13 }, strengthItem: { width: 14, height: 2.5, marginLeft: 8, borderRadius: 3, backgroundColor: '#CCC' }, })