JQuery-based password Strength Verification Code

Source: Internet
Author: User


Because it is a JQuery-based control, of course, the JQuery library is required, and a JS of this control is required. JQuery JS you can download to the official website: http://code.jquery.com/jquery-1.4.2.min.js
The JS file of this control: password_strength_plugin.js
Password_strength_plugin.js
Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. shortPass = 'too short ';
$. Fn. badPass = 'weak ';
$. Fn. goodPass = 'good ';
$. Fn. strongPass = 'strong ';
$. Fn. samePassword = 'username and Password identical .';
$. Fn. resultStyle = "";
$. Fn. passStrength = function (options ){
Var defaults = {
Required pass: "required pass", // optional
BadPass: "badPass", // optional
GoodPass: "goodPass", // optional
StrongPass: "strongPass", // optional
BaseStyle: "testresult", // optional
Userid: "", // required override
Messageloc: 1 // before = 0 or after = 1
};
Var opts = $. extend (defaults, options );
Return this. each (function (){
Var obj = $ (this );
$ (Obj). unbind (). keyup (function ()
{
Var results = $. fn. teststrength ($ (this). val (), $ (opts. userid). val (), opts );
If (opts. messageloc = 1)
{
$ (This). next ("." + opts. baseStyle). remove ();
$ (This). after ("<span class = \" "+ opts. baseStyle +" \ "> <span> </span> ");
$ (This). next ("." + opts. baseStyle). addClass ($ (this). resultStyle). find ("span"). text (results );
}
Else
{
$ (This). prev ("." + opts. baseStyle). remove ();
$ (This). before ("<span class = \" "+ opts. baseStyle +" \ "> <span> </span> ");
$ (This). prev ("." + opts. baseStyle). addClass ($ (this). resultStyle). find ("span"). text (results );
}
});
// FUNCTIONS
$. Fn. teststrength = function (password, username, option ){
Var score = 0;
// Password <4
If (password. length <4) {this. resultStyle = option. Allow pass; return $ (this). Allow pass ;}
// Password = user name
If (password. toLowerCase () = username. toLowerCase () {this. resultStyle = option. badPass; return $ (this). samePassword ;}
// Password length
Score + = password. length * 4;
Score + = ($. fn. checkRepetition (1, password). length-password. length) * 1;
Score + = ($. fn. checkRepetition (2, password). length-password. length) * 1;
Score + = ($. fn. checkRepetition (3, password). length-password. length) * 1;
Score + = ($. fn. checkRepetition (4, password). length-password. length) * 1;
// Password has 3 numbers
If (password. match (/(. * [0-9]. * [0-9]. * [0-9])/) {score + = 5 ;}
// Password has 2 symbols
If (password. match (/(.*[!, @, #, $, %, ^ ,&,*,?, _, ~]. *[!, @, #, $, %, ^ ,&,*,?, _, ~]) /) {Score + = 5 ;}
// Password has Upper and Lower chars
If (password. match (/([a-z]. * [A-Z]) | ([A-Z]. * [a-z])/) {score + = 10 ;}
// Password has number and chars
If (password. match (/([a-zA-Z])/) & password. match (/([0-9])/) {score + = 15 ;}
//
// Password has number and symbol
If (password. match (/([!, @, #, $, %, ^ ,&,*,?, _, ~]) /) & Password. match (/([0-9])/) {score + = 15 ;}
// Password has char and symbol
If (password. match (/([!, @, #, $, %, ^ ,&,*,?, _, ~]) /) & Password. match (/([a-zA-Z])/) {score + = 15 ;}
// Password is just a numbers or chars
If (password. match (/^ \ w + $/) | password. match (/^ \ d + $/) {score-= 10 ;}
// Verifying 0 <score <100
If (score <0) {score = 0 ;}
If (score> 100) {score = 100 ;}
If (score <34) {this. resultStyle = option. badPass; return $ (this). badPass ;}
If (score <68) {this. resultStyle = option. goodPass; return $ (this). goodPass ;}
This. resultStyle = option. strongPass;
Return $ (this). strongPass;
};
});
};
}) (JQuery );
$. Fn. checkRepetition = function (pLen, str ){
Var res = "";
For (var I = 0; I <str. length; I ++)
{
Var repeated = true;
For (var j = 0; j <pLen & (j + I + pLen) <str. length; j ++ ){
Repeated = repeated & (str. charAt (j + I) = str. charAt (j + I + pLen ));
}
If (j <pLen) {repeated = false ;}
If (repeated ){
I + = pLen-1;
Repeated = false;
}
Else {
Res + = str. charAt (I );
}
}
Return res;
};

Css file of this control:
Style.css
Copy codeThe Code is as follows:
Td label {
Font-size: 14px;
Font-weight: bold;
Color: #666;
Font-family: arail, helvetica, san-serif;
}
Input {
Height: 28px;
Width: 200px;
Border: 1px solid # ccc;
Font-size: 16px;
Font-weight: bold;
Color: #666;
Padding: 7px 0 0 4px;
}
/* Advanced styles */
. Top_testresult {
Font-weight: bold;
Font-size: 13px;
Font-family: arail, helvetica, san-serif;
Color: #666;
Padding: 0;
Margin: 0 0 2px 0;
}
. Top_testresult span {
Padding: 6px;
Margin: 0;
}
. Top_shortPass {
Background: # edabab;
Border: 1px solid # bc0000;
Display: block;
}
. Top_shortPass span {
}
. Top_badPass {
Background: # edabab;
Border: 1px solid # bc0000;
Display: block;
}
. Top_badPass span {
}
. Top_goodPass {
Background: # ede3ab;
Border: 1px solid # bc9f00;
Display: block;
}
. Top_goodPass span {
}
. Top_strongPass {
Background: # d3edab;
Border: 1px solid #73bc00;
Display: block;
}
. Top_strongPass span {
}
/* Result style */
. Testresult {
Font-weight: bold;
Font-size: 13px;
Font-family: arial, helvetica, san-serif;
Color: #666;
Padding: 0px 0px 12px 10px;
Margin-left: 10px;
Display: block;
Height: 28px;
Float: left;
}
. Testresult span {
Padding: 10px 20px 12px 10px;
Margin: 0px 0px 0px 20px;
Display: block;
Float: right;
White-space: nowrap;
}
. Transfer pass {
Background: url (../images/red.png) no-repeat 0 0;
}
. Repeated pass span {
Background: url (../images/red.png) no-repeat top right;
}
. BadPass {
Background: url (../images/red.png) no-repeat 0 0;
}
. BadPass span {
Background: url (../images/red.png) no-repeat top right;
}
. GoodPass {
Background: url (../images/yellow.png) no-repeat 0 0;
}
. GoodPass span {
Background: url (../images/yellow.png) no-repeat top right;
}
. StrongPass {
Background: url (../images/green.png) no-repeat 0 0;
}
. StrongPass span {
Background: url (../images/green.png) no-repeat top right;
}

Head code
Head
Copy codeThe Code is as follows:
<Title> No title page </title>
<Script type = "text/javascript" src = "js/jquery-1.4.2.min.js"> </script>
<! -- Custom select plugin js -->
<Script type = "text/javascript" src = "js/password_strength_plugin.js"> </script>
<Link rel = "stylesheet" type = "text/css" href = "css/style.css">
<Script>
$ (Document). ready (function (){
// BASIC
$ (". Password_test"). passStrength ({
Userid: "# user_id"
});
// ADVANCED
$ (". Password_adv"). passStrength ({
Transfer pass: "top_transfer pass ",
BadPass: "top_badPass ",
GoodPass: "top_goodPass ",
StrongPass: "top_strongPass ",
BaseStyle: "top_testresult ",
Userid: "# user_id_adv ",
Messageloc: 0
});
});
</Script>

Body code
Body
Copy codeThe Code is as follows:
<Body>
<Table cellpadding = "2" cellspacing = "0" border = "0">
<Tr>
<Td align = "right"> <label> User Name: </label> </td>
<Td> <input type = "text" name = "user_name" id = "user_id_adv"/> </td>
</Tr>
<Tr>
<Td align = "right"> <label> Password: </label> </td>
<Td> <input type = "password" name = "pass_word" class = "password_adv"/> </td>
</Tr>
</Table>
</Body>

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.