Ajax注册验证js代码
分享jquery网站:http://www.css88.com/jqapi-1.9/focusout/
1 $(document).ready(function() { 2 3 var bool_user = false; 4 var bool_mail = false; 5 var bool_pwd1 = false; 6 var bool_pwd2 = false; 7 var bool_code = false; 8 9 $("#username").focusin(function(){ 10 $('#p_username').css('color', '#666'); 11 }); 12 13 $("#username").focusout(function() { 14 15 $('#p_username').css('color', '#A5A5A5'); 16 17 var pattern = /^[A-Za-z0-9_\u4e00-\u9fa5]+$/; 18 var username = $("#username").val(); 19 20 var username_tmp = username.replace(/[^\x00-\xff]/g, 'xx'); 21 22 $("#div_usrename").html("内容"); 23 if (username != '') { 24 $("#div_usrename").empty(); 25 26 if (username_tmp.length < 4 || username_tmp.length > 20) { 27 28 $("#div_usrename").addClass("err1"); 29 $("#div_usrename").append("内容"); 30 } else if (!pattern.test(username)) { 31 $("#div_usrename").addClass("err1"); 32 $("#div_usrename").append("内容"); 33 } else { 34 35 $.ajax({ 36 type: 'post', 37 url: '/site/checkUsername', 38 data: "username=" + username, 39 success: function(data) { 40 data = eval("(" + data + ")"); 41 if (data.flag == 1) { 42 $("#div_usrename").removeClass('err1').addClass('rig1').html("内容"); 43 bool_user = true; 44 } else { 45 $("#div_usrename").addClass("err1"); 46 $("#div_usrename").html("内容"); 47 } 48 } 49 }) 50 } 51 } else { 52 $("#div_usrename").addClass('err1').html("内容"); 53 } 54 }); 55 56 57 $("#email").focusin(function(){ 58 $('#p_email').css('color', '#666'); 59 }); 60 61 $("#email").focusout(function() { 62 63 $('#p_email').css('color', '#A5A5A5'); 64 65 var pattern = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; 66 67 var email = $("#email").val(); 68 69 if (email != '') { 70 $("#div_email").empty(); 71 $("#div_email").html("内容"); 72 if (pattern.test(email)) { 73 $.ajax({ 74 type: 'post', 75 url: '/site/checkEmail', 76 data: "email=" + email, 77 success: function(data) { 78 data = eval("(" + data + ")"); 79 if (data.flag == 1) { 80 $("#div_email").removeClass('err1').addClass('rig1').html(''); 81 bool_mail = true; 82 } else if(data.flag == -1){ 83 $("#div_email").addClass('err1').html("内容"); 84 } else if(data.flag == -2){ 85 $("#div_email").addClass('err1').html("内容"); 86 } else if(data.flag == -3){ 87 $("#div_email").addClass('err1').html("内容"); 88 } else if(data.flag == -4){ 89 $("#div_email").addClass('err1').html("内容"); 90 } else { 91 $("#div_email").addClass('err1').html("内容"); 92 } 93 } 94 }) 95 } else { 96 $("#div_email").addClass('err1').html("内容"); 97 } 98 } else { 99 $("#div_email").addClass('err1').html("内容"); 100 } 101 }); 102 103 $("#pwd1").focusin(function(){ 104 $('#p_pwd1').css('color', '#666'); 105 }); 106 107 $("#pwd1").focusout(function(){ 108 $('#p_pwd1').css('color', '#A5A5A5'); 109 }); 110 111 $("#pwd1").keyup(function() { 112 113 $('#p_pwd1').css('color', '#666'); 114 115 var username = $('#username').val(); 116 var pwd1 = $("#pwd1").val(); 117 118 if (pwd1 != '') { 119 $("#div_pwd1").empty(); 120 121 var score = testpass(pwd1,username); 122 123 if (score == -4) { 124 $('#pwdsbox1').removeClass('pwdsb').addClass('pwds'); 125 $('#pwdsbox2').removeClass('pwdsb').addClass('pwds'); 126 $('#pwdsbox3').removeClass('pwdsb').addClass('pwds'); 127 $("#div_pwd1").addClass('err1').html("内容"); 128 } else if (score == -2 ) { 129 $('#pwdsbox1').removeClass('pwdsb').addClass('pwds'); 130 $('#pwdsbox2').removeClass('pwdsb').addClass('pwds'); 131 $('#pwdsbox3').removeClass('pwdsb').addClass('pwds'); 132 $("#div_pwd1").addClass('err1').html("内容"); 133 } else { 134 135 if(score >= 70){ 136 $('#pwdsbox1').removeClass('pwds').addClass('pwdsb'); 137 $('#pwdsbox2').removeClass('pwds').addClass('pwdsb'); 138 $('#pwdsbox3').removeClass('pwds').addClass('pwdsb'); 139 bool_pwd1 = true; 140 $("#div_pwd1").removeClass('err1').addClass('rig1').html(""); 141 } else if(score >= 30){ 142 $('#pwdsbox1').removeClass('pwds').addClass('pwdsb'); 143 $('#pwdsbox2').removeClass('pwds').addClass('pwdsb'); 144 $('#pwdsbox3').removeClass('pwdsb').addClass('pwds'); 145 bool_pwd1 = true; 146 $("#div_pwd1").removeClass('err1').addClass('rig1').html(""); 147 } else if(score >= 1){ 148 $("#div_pwd1").addClass('err1').html("内容"); 149 $('#pwdsbox1').removeClass('pwds').addClass('pwdsb'); 150 $('#pwdsbox2').removeClass('pwdsb').addClass('pwds'); 151 $('#pwdsbox3').removeClass('pwdsb').addClass('pwds'); 152 } else { 153 $('#pwdsbox1').removeClass('pwdsb').addClass('pwds'); 154 $('#pwdsbox2').removeClass('pwdsb').addClass('pwds'); 155 $('#pwdsbox3').removeClass('pwdsb').addClass('pwds'); 156 } 157 } 158 } else { 159 $('#pwdsbox1').removeClass('pwdsb').addClass('pwds'); 160 $('#pwdsbox2').removeClass('pwdsb').addClass('pwds'); 161 $('#pwdsbox3').removeClass('pwdsb').addClass('pwds'); 162 $("#div_pwd1").addClass('err1').html("内容"); 163 } 164 }); 165 166 167 $("#pwd2").focusin(function(){ 168 $('#p_pwd2').css('color', '#666'); 169 }); 170 171 $("#pwd2").focusout(function() { 172 173 $('#p_pwd2').css('color', '#A5A5A5'); 174 175 var pwd1 = $("#pwd1").val(); 176 var pwd2 = $("#pwd2").val(); 177 178 if (pwd2 != '') { 179 if (pwd1 == pwd2) { 180 $("#div_pwd2").removeClass('err1').addClass('rig1').html(""); 181 bool_pwd2 = true; 182 } else { 183 $("#div_pwd2").addClass('err1').html("内容"); 184 } 185 186 } else { 187 $("#div_pwd2").addClass('err1').html("内容"); 188 } 189 }); 190 191 $("#code").focusout(function() { 192 var code = $("#code").val(); 193 if (code != '') { 194 $("#div_code").html("内容"); 195 $.ajax({ 196 type: 'post', 197 url: '/site/checkVerifycode', 198 data: "code=" + code, 199 success: function(data) { 200 data = eval("(" + data + ")"); 201 if (data.flag == 1) { 202 $("#div_code").removeClass('err1').addClass('rig1').html(""); 203 bool_code = true; 204 } else { 205 $("#div_code").addClass('err1').html("内容"); 206 } 207 } 208 }) 209 } else { 210 $("#div_code").addClass('err1').html("内容"); 211 } 212 }); 213 214 $("#submit").click(function() { 215 if (bool_user && bool_pwd1 && bool_pwd2 && bool_code && bool_mail) { 216 return true; 217 } else { 218 if ($("#username").val() == "") { 219 $("#div_usrename").addClass('err1').html("请输入用户å"); 220 } 221 if ($("#pwd1").val() == "") { 222 $("#div_pwd1").addClass('err1').html("内容"); 223 } 224 if ($("#pwd2").val() == "") { 225 $("#div_pwd2").addClass('err1').html("内容"); 226 } 227 if ($("#email").val() == "") { 228 $("#div_email").addClass('err1').html("内容"); 229 } 230 if ($("#code").val() == "") { 231 $("#div_code").addClass('err1').html("内容"); 232 } 233 return false; 234 } 235 }); 236 }); 237 238 function testpass(password, username) { 239 var score = 0; 240 if (password.length < 6 || password.length > 24) { 241 return - 4; 242 } 243 if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()) { 244 return - 2 245 } 246 score += password.length * 4; 247 score += (repeat(1, password).length - password.length) * 1; 248 score += (repeat(2, password).length - password.length) * 1; 249 score += (repeat(3, password).length - password.length) * 1; 250 score += (repeat(4, password).length - password.length) * 1; 251 if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) { 252 score += 5; 253 } 254 if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) { 255 score += 5; 256 } 257 if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) { 258 score += 10; 259 } 260 if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) { 261 score += 15; 262 } 263 if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)) { 264 score += 15; 265 } 266 if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)) { 267 score += 15; 268 } 269 if (password.match(/^\w+$/) || password.match(/^\d+$/)) { 270 score -= 10; 271 } 272 if (score < 0) { 273 score = 0; 274 } 275 if (score > 100) { 276 score = 100; 277 } 278 return score; 279 280 } 281 282 function repeat(len, str) { 283 var res = ""; 284 for (var i = 0; i < str.length; i++) { 285 var repeated = true; 286 for (var j = 0, 287 max = str.length - i - len; j < len && j < max; j++) { 288 repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len)); 289 } 290 if (j < len) repeated = false; 291 if (repeated) { 292 i += len - 1; 293 repeated = false; 294 } else { 295 res += str.charAt(i); 296 } 297 } 298 return res; 299 }