密碼為6-20位數字、字母或下劃線,以字母開頭,至少包括其中兩種
密碼設定8到20字元怎麼設定
最近工作中遇到一個驗證密碼格式的正則問題,密碼格式要求如下:
密碼為6-20位數字、字母或下劃線,至少包括其中兩種,以字母開頭
然後自己寫過幾個,也在網上找過,都沒完全符合該密碼規則。後來將之前用過的幾種規則進行各種綜合、試錯,終於找到了能驗證該密碼規則的正則。
廢話不多說,上正則:
/^[a-zA-Z](?![a-zA-Z]+$)\w{5,19}$/
【密碼為6-20位數字、字母或下劃線,至少包括其中兩種,以字母開頭】正則規則圖
下面是幾種對於該正則的測試:
const passwordReg = /^[a-zA-Z](?![a-zA-Z]+$)\w{5,19}$/passwordReg。test(‘aaaaa1’) // truepasswordReg。test(‘aaaaa_’) // truepasswordReg。test(‘a111111’) // truepasswordReg。test(‘a_______’) // truepasswordReg。test(‘Aaaaa1’) // truepasswordReg。test(‘1aaaaa1’) // falsepasswordReg。test(‘_aaaaa1’) // falsepasswordReg。test(‘aaaaaa’) // falsepasswordReg。test(‘111111’) // falsepasswordReg。test(‘______’) // false