React實現(xiàn)基于Antd密碼強度校驗組件示例詳解
引言
最近在開發(fā) Nest
和 Umi
技術(shù)棧的個人項目,在用戶管理模塊需要用到一個密碼強度校驗組件,在網(wǎng)上尋找一方資料,沒有找到自己想要的,特此自己造輪子!
效果預(yù)覽
組件思想
- 既然是密碼強度校驗,那么強度就必須有個梯度,這個時候就必須找到一個合適的效果。
- 我們有兩種方向:① 組件庫找個合適的 UI , ② 自己開發(fā)造輪子
- 經(jīng)過一番摸索,
Antd
的Progress
組件進入了我的視野:
于是我決定基于這個組件改造一番!
組件開發(fā)
- 在目錄
/src/components
新建StrengthMeter/index.tsx
文件,開發(fā)基本結(jié)構(gòu)。
/* * @Description: 密碼強度組件 * @Version: 2.0 * @Author: Cyan * @Date: 2023-01-09 17:15:19 * @LastEditors: Cyan * @LastEditTime: 2023-01-16 15:40:45 */ import type { FC } from 'react' import { Progress, Form, Row, Col } from 'antd'; import { ProFormText } from '@ant-design/pro-components'; // antd 高級組件 import zxcvbn from 'zxcvbn'; // 密碼強度校驗 const StrengthMeter: FC = () => { // 獲取上下文 form 實例 const form = Form.useFormInstance(); // 監(jiān)聽密碼的改變 const password = Form.useWatch('password', form); /** * @description: 監(jiān)聽密碼強度相應(yīng)變化 * @param {string} password * @return {*} * @author: Cyan */ const watchStrength = (password: string): number => { const analysisValue = zxcvbn(password) // score得分只有0~4,且只有整數(shù)范圍并沒有小數(shù) return (analysisValue.score + 1) * 20 } return ( <> {/* 密碼 */} <ProFormText.Password label="密碼" name="password" rules={[{ required: true, min: 6, max: 12, message: "請輸入密碼" }]} /> {/* 確認密碼 */} <ProFormText.Password label="確認密碼" name="confirmPassword" fieldProps={{ visibilityToggle: false }} rules={[ { required: true, message: "請輸入確認密碼" }, ({ getFieldValue }) => ({ validator(_, value) { if (!value || getFieldValue('password') === value) { return Promise.resolve(); } return Promise.reject(new Error("兩次密碼輸入不一致")); }, }) ]} /> {/* 顯示密碼強度 */} <Progress percent={password ? watchStrength(password) : 0} steps={5} strokeColor={['#e74242', '#EFBD47', '#ffa500', '#1bbf1b', '#008000']} showInfo={false} /> <Row justify="space-around"> { ['非常弱', '弱', '一般', '強', '非常強'].map(value => <Col span={4} key={value}>{value} </Col>) } </Row> </> ) } export default StrengthMeter
- 此時的效果是這樣的:
由于 Progress
的 ant-progress-steps-item
無法自動撐開,我們需要新建一個 index.module.less
文件做樣式穿透:
.process-steps{ width:100%; text-align: center; :global(.ant-progress){ width:100% } :global(.ant-progress .ant-progress-steps-item){ width:calc(20% - 2px) !important } }
然后引入樣式并綁定類名:
import styles from './index.module.less' <div className={styles['process-steps']}> <Progress percent={password ? watchStrength(password) : 0} steps={5} strokeColor={['#e74242', '#EFBD47', '#ffa500', '#1bbf1b', '#008000']} showInfo={false} /> </div> <Row justify="space-around" className={styles['process-steps']}> { ['非常弱', '弱', '一般', '強', '非常強'].map(value => <Col span={4} key={value}>{value}</Col>) } </Row>
這時候就能得到我們想要的效果了,接下來我們要校驗密碼強度。
3. 這里我們要用到一個庫:zxcvbn,頁面引入
import zxcvbn from 'zxcvbn';
zxcvbn
是個函數(shù),接收一個參數(shù),參數(shù)就是字符串密碼。
zxcvbn("abc123456");
該函數(shù)返回一個對象,其中與密碼強度相關(guān)的屬性有:guesses
、guesses_log10
、score
。
那么這三個屬性,我們應(yīng)該怎么選擇呢?
①: guesses
值很大,不利于我們判斷。
②: guesses_log10
的值越大越安全,根據(jù)測試,值在 12 以上就很安全了。
③: score
的取值范圍只有整數(shù) 0~4,值越大越安全。
如果業(yè)務(wù)考慮的場景比較多,建議使用 guesses_log10
,這里我們封裝使用 score
。
4. 使用 Form.useWatch
監(jiān)聽 password
的變化:
// 獲取上下文 form 實例 const form = Form.useFormInstance(); // 監(jiān)聽密碼的改變 const password = Form.useWatch('password', form);
編寫一個函數(shù)解析 password
:
const watchStrength = (password: string): number => { const analysisValue = zxcvbn(password) // score得分只有0~4,且只有整數(shù)范圍并沒有小數(shù) return (analysisValue.score + 1) * 20 }
綁定到 Progress
組件:
<Progress percent={password ? watchStrength(password) : 0} steps={5} strokeColor={['#e74242', '#EFBD47', '#ffa500', '#1bbf1b', '#008000']} showInfo={false} />
到這里,我們的任務(wù)就完成了,我們一起看看實際效果吧:
倉庫地址:Xmw-Admin
以上就是React實現(xiàn)基于Antd密碼強度校驗組件示例詳解的詳細內(nèi)容,更多關(guān)于React Antd密碼強度校驗的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React-Native使用Mobx實現(xiàn)購物車功能
本篇文章主要介紹了React-Native使用Mobx實現(xiàn)購物車功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09React Native時間轉(zhuǎn)換格式工具類分享
這篇文章主要為大家分享了React Native時間轉(zhuǎn)換格式工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10react項目中redux的調(diào)試工具不起作用的解決
這篇文章主要介紹了react項目中redux的調(diào)試工具不起作用的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01