Password strength javascript validation code from Google
Last Update:2017-02-28
Source: Internet
Author: User
Google 1 <script type= "Text/javascript" >
2 var agt = Navigator.userAgent.toLowerCase ();
3 var is_op = (Agt.indexof ("opera")!=-1);
4 var Is_ie = (Agt.indexof ("MSIE")!=-1) && document.all &&!is_op;
5 var is_ie5 = (Agt.indexof ("MSIE 5")!=-1) && document.all &&!is_op;
6
7 function Createxmlhttpreq (handler) {
8 var xmlhttp = null;
9 if (Is_ie) {
var control = (IS_IE5)? "Microsoft.XMLHTTP": "Msxml2.xmlhttp";
One try {
XMLHTTP = new ActiveXObject (control);
Xmlhttp.onreadystatechange = handler;
' Catch ' (ex) {
Alert ("You are need to enable Active Scripting and ActiveX controls");
16}
17
%} else {
XMLHTTP = new XMLHttpRequest ();
Xmlhttp.onload = handler;
Xmlhttp.onerror = handler;
22}
return XMLHTTP;
24}
25
26
function xmlhttppost (XMLHTTP, URL, data) {
try {
Xmlhttp.open ("POST", url, True);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
Xmlhttp.send (data);
32
+} catch (ex) {
/Do Nothing
35}
36}
37
38
var myxmlhttp;
Ratingmsgs = new Array (6);
Ratingmsgcolors = new Array (6);
Barcolors = new Array (6);
43
Ratingmsgs[0] = "Too short";
RATINGMSGS[1] = "weak";
RATINGMSGS[2] = "General";
RATINGMSGS[3] = "very good";
RATINGMSGS[4] = "excellent";
RATINGMSGS[5] = "Not rated";
50
Wuyi Ratingmsgcolors[0] = "#676767";
RATINGMSGCOLORS[1] = "#aa0033";
RATINGMSGCOLORS[2] = "#f5ac00";
RATINGMSGCOLORS[3] = "#6699cc";
RATINGMSGCOLORS[4] = "#008000";
RATINGMSGCOLORS[5] = "#676767";
57
Barcolors[0] = "#dddddd";
BARCOLORS[1] = "#aa0033";
BARCOLORS[2] = "#ffcc33";
BARCOLORS[3] = "#6699cc";
BARCOLORS[4] = "#008000";
BARCOLORS[5] = "#676767";
64
Createratepasswdreq function ()
66 {
The var passwd = getelement (' passwd '). Value;
var email = getelement (' email '). Value;
LastName var = getelement (' LastName '). Value;
var FirstName = getelement (' firstname '). Value;
The var min_passwd_len = 6;
72
73
if (Passwd.length < Min_passwd_len)
75 {
The IF (Passwd.length > 0)
77 {
drawbar (0);
79}
Or else
81 {
Resetbar ();
83}
84}
Or else
86 {
passwd = Escape (passwd);
The var params = ' passwd= ' +passwd+ ' &email= ' +email+ ' &firstname= ' +firstname+ ' ' &lastname= ';
Myxmlhttp = Createxmlhttpreq (Ratepasswdxmlhttphandler);
Xmlhttppost (Myxmlhttp, "Https://www.google.com/accounts/RatePassword", params);
91}
92}
93
getelement function (name)
95 {
if (document.all)
97 {
document.all return (name);
99}
document.getElementById return (name);
101}
102
Ratepasswdxmlhttphandler function ()
The {//Can ' t check status since Safari doesn ' t support it
if (myxmlhttp.readystate!= 4)
106 {
return;
108}
109 rating = parseint (myxmlhttp.responsetext);
Drawbar (rating);
111}
112
113 function drawbar (rating)
114 {
The var Posbar = getelement (' Posbar ');
116 var Negbar = getelement (' Negbar ');
117 var passwdrating = getelement (' passwdrating ');
118 var barlength = getelement (' Passwdbar '). Width;
119
If (rating >= 0 && rating <= 4)
121 {//we successfully got a rating
122 Posbar.style.width = BARLENGTH/4 * rating + "px";
123 Negbar.style.width = BARLENGTH/4 * (4-rating) + "px";
124}
Or else
126 {
127 posbar.style.width = "0px";
128 negbar.style.width = barlength + "px";
129 rating = 5; Not rated Rating
130}
131 Posbar.style.background = barcolors[rating]
132 passwdrating.innerhtml = "<font color= '" + ratingmsgcolors[rating] + "' >" + ratingmsgs[rating] + "</font>" ;
133}
134
135
136//resets The password strength bar back to it initial state without any message showing.
137 function Resetbar ()
138 {
139 var Posbar = getelement (' Posbar ');
140 var Negbar = getelement (' Negbar ');
passwdrating var = getelement (' passwdrating ');
barlength var = getelement (' Passwdbar '). Width;
143
144 posbar.style.width = "0px";
145 negbar.style.width = barlength + "px";
146 passwdrating.innerhtml = "";
147}
148
149 </script>
150
151 <table width= "60%" border= "0" >
152 <tr>
153 <td width= "30%" >
154 <input type= "hidden" value= "" id= "FirstName" size= ">
<input type= "hidden" value= "id=" LastName "size=" >
156 <input type= "hidden" id= "Email" value= "" size= ">
157 <input type= "password" id= "Passwd" value= "" onkeyup= "Createratepasswdreq ()" Size= ">
158 </td>
159 <TD width= "70%" >
160 <table cellpadding= "0" cellspacing= "0" border= "0" >
161 <tr>
162 <TD width= "id=" Passwdbar ">
163 <table cellpadding= "0" cellspacing= "0" border= "0" >
164 <tr>
165 <td nowrap valign= "Top" height= "15px" ><font color= "#808080" size= "-1" face= " Arial, Sans-serif "><div id=" passwdrating "></div></font></td>
166 </tr>
167 <tr>
<TD height= "3px" ></td>
169 </tr>
170 <tr>
171 <TD colspan= "2" >
172 <table cellpadding= "0" bgcolor= "#ffffff" cellspacing= "0" border= "0" width= "200" >
173 <tr>
174 <td width= "0%" id= "Posbar" bgcolor= "#e0e0e0" height= "4px" ></td> ;
175 <TD width= "100%" id= "Negbar" bgcolor= "#e0e0e0" height= "4px" ></td& Gt
176 </tr>
177 </table>
178 </td>
179 </tr>
180 </table>
181 </td>
</tr>
183 </table>
184 </td>
185 </tr>
186 </table>
187