Small Example of verifying the strength of passwords in js

Source: Internet
Author: User

Page code:

Copy codeThe Code is as follows:
<Table>
<Tr> <td> <input type = "text" id = "txtPwd"/> </td> </tr>
<Tr> <td>
<Table id = "pwdLever">
<Tr>
<Td> weak </td>
<Td> moderate </td>
<Td> strong </td>
</Tr>
</Table>
</Td> </tr>
</Table>

Style:

Copy codeThe Code is as follows:
<Style type = "text/css">
# PwdLever td
{
Background-color: Gray;
Width: 45px;
Text-align: center;
}
</Style>

Js Code:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Window. onload = function (){
Var textInput = document. getElementById ("txtPwd ");
// Register a key release event in the password input box
TextInput. onkeyup = function (){
Var pwdValue = this. value;
Var num = pwdChange (pwdValue );
Var tds = document. getElementById ("pwdLever"). getElementsByTagName ("td ");
// Change the password color
If (num = 0 | num = 1 ){
Tds [0]. style. backgroundColor = "red ";
Tds [1]. style. backgroundColor = "gray ";
Tds [2]. style. backgroundColor = "gray ";
}
Else if (num = 2 ){
Tds [0]. style. backgroundColor = "red ";
Tds [1]. style. backgroundColor = "red ";
Tds [2]. style. backgroundColor = "gray ";
}
Else if (num = 3 ){
Tds [0]. style. backgroundColor = "red ";
Tds [1]. style. backgroundColor = "red ";
Tds [2]. style. backgroundColor = "red ";
}
Else {
Tds [0]. style. backgroundColor = "gray ";
Tds [1]. style. backgroundColor = "gray ";
Tds [2]. style. backgroundColor = "gray ";
}
}
}
Function pwdChange (v ){
Var num = 0;
Var reg =/\ d/; // if there is a number
If (reg. test (v )){
Num ++;
}
Reg =/[a-zA-Z]/; // if there are letters
If (reg. test (v )){
Num ++;
}
Reg =/[^ 0-9a-zA-Z]/; // if there are special characters
If (reg. test (v )){
Num ++;
}
If (v. length <6) {// if the password is less than 6
Num --;
}
Return num;
}
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.