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][email protected]([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 }

时间: 11-18

Ajax注册验证js代码的相关文章

ajax同步请求JS代码

ajax同步请求JS代码 <script type="text/javascript"> var txt = document.getElementById('txt'); //1.创建XMLHttpRequest对象 var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); }else { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } /

Ajax注册验证用户名是否存在 ——引自百度经验

Ajax注册验证用户名是否存在 http://jingyan.baidu.com/article/a948d6515fdf870a2dcd2e85.html

MVC 注册验证参考代码

using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication5注册验证.Models { public class ZhuceBF { private mydboDataContext _Context = new mydboDataContext(); public List<zhuce> Select() { return _Cont

Ajax中通过JS代码自动获取表单元素值的示例代码

我们在使用Ajax的时候,通常需要获取表单元素值,然后发送给后台的服务器端程序处理.如果表单元素不多的情况我们常常会通过GET方式来获取表单元素值,但如果表单元素非常多,此时就需要用POST方式来获取表单元素值,那么如何来获取表单元素值呢?下面给出一段JS代码即可自动获取表单元素的值了 http://www.qidian.com/BookReader/1839917,60421843.aspx http://www.qidian.com/BookReader/1839917,60422045.a

中国第二代身份证验证js代码

下面这部分代码截取自盛大的某个网页,具体我就不给url了,下面是对应的js代码: iW = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); iSum = 0; for( i=0;i<17;i++){ iC = v_card.charAt(i) ; iVal = parseInt(iC); iSum += iVal * iW[i]; } iJYM = iSum % 11; var sJYM = ''; if(iJYM == 0) sJYM = &qu

tp框架实现ajax注册验证 静态验证

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

表单验证js代码

请转载此文的朋友务必附带原文链接,谢谢. 原文链接:http://xuyran.blog.51cto.com/11641754/1861926 <form class="mui-input-group">     <div class="mui-input-row">         <input type="tel"  placeholder="手机" required="requir

Flask博客类登录注册验证模块代码(十四)

1 文件系统 blog #博客类 App forms #表单 __init__.py user.py models #模型 __init__.py user.py static #静态文件 templates #模板 common #基类模板 base.html email #邮件 email.html errors #错误 error.html main #主页 index.html user #用户登录注册 login.html register.html views #视图蓝本 __ini

将form转为ajax提交的js代码

在html中插入下面的代码: 函数ajaxSubmit是submit的ajax形式. 注意:这里面使用到了jquery库 //<!--将form中的值转换为键值对.--> function getFormJson(frm) { var o = {}; var a = $(frm).serializeArray(); $.each(a, function () { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this