这篇文章主要介绍了Ajax异步方式实现登录与验证,感兴趣的小伙伴们可以参考一下
本文实例讲述了Ajax异步方式实现登录与验证的详细代码,分享给大家供大家参考。具体如下:
登录代码
这个是使用Bootstrap3的组件功能实现的
登录
异步登录请求的代码
使用到的验证js代码Validation.js
Validator = { Require : {expression:/.+/,errorMsg:"该字段不能为空"}, Email : {expression:/^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/,errorMsg:"Email格式不正确"}, Mobile:{expression:/^(1[3|5|8]{1}\d{9})$/,errorMsg:"手机格式不正确"}, TelePhone:{expression:/^(((0\d{2,3}-)?\d{7,8}(-\d{4})?))$/,errorMsg:"电话号码格式不正确"}, IdCard:{expression:/^\d{15}(\d{2}[A-Za-z0-9])?$/,errorMsg:"身份证号码格式不正确"}, Number:{expression:/^-?(\d+|[1-9]\d*\.\d+|0\.\d*[1-9]\d*|0?\.0+|0)$/,errorMsg:"仅支持数字"}, English:{expression:/^[A-Za-z]+$/,errorMsg:"仅支持英文字符"}, Chinese:{expression:/^[\u0391-\uFFE5]+$/,errorMsg:"仅支持中文字符"}, URL:{expression:/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?-&_~`@[\]\':+!]*([^<>\"\"])*$/,errorMsg:"URL地址格式不正确"}, Regex:{errorMsg:"格式不正确"}, UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/, IsSafe : function(str){return !this.UnSafe.test(str);}, SafeString : {expression:"this.IsSafe(value)",errorMsg:"仅支持中文字符"}, ErrorItem : [document.forms[0]], ErrorMessage : ["以下原因导致提交失败:\t\t\t\t"], Validate : function(theForm, mode){ var obj = theForm || event.srcElement; var count = obj.elements.length; this.ErrorMessage.length = 1; this.ErrorItem.length = 1; this.ErrorItem[0] = obj; for(var i=0;i1){ var content = this.ErrorMessage[1].replace(/\d+:/," "); var $element = $(this.ErrorItem[1]); var name = $element.attr('name'); if(name.indexOf('DTO') != -1){ name = name.split('.')[1]; } var $targetElement = $element; var $tempElement = $targetElement.closest('form').find('#' + name + 'ID'); if($tempElement.hasClass('select')){ $targetElement = $tempElement; } showErrorMessage($element.closest('.modal'), $targetElement, content); return false; } return true; }, limit : function(len,min, max){ min = min || 0; max = max || Number.MAX_VALUE; return min <= len && len <= max; }, LenB : function(str){ return str.replace(/[^\x00-\xff]/g,"**").length; }, ClearState : function(elem){ with(elem){ if(style.color == "red") style.color = ""; var ligertipid = getAttribute("ligertipid"); if(ligertipid && ligertipid != ""){ removeAttribute("ligertipid"); $(elem).ligerHideTip(); } } }, AddError : function(index, str){ this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index]; this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str; }, Exec : function(op, reg){ return new RegExp(reg,"g").test(op); }, compare : function(op1,operator,op2){ switch (operator) { case "NotEqual": return (op1 != op2); case "GreaterThan": return (op1 > op2); case "GreaterThanEqual": return (op1 >= op2); case "LessThan": return (op1 =0;i--) if(groups[i].checked) hasChecked++; return min <= hasChecked && hasChecked <= max; }, IsDate : function(op, formatString){ formatString = formatString || "ymd"; var m, year, month, day; switch(formatString){ case "ymd" : m = op.match(new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$")); if(m == null ) return false; day = m[6]; month = m[5]--; year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10)); break; case "dmy" : m = op.match(new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$")); if(m == null ) return false; day = m[1]; month = m[3]--; year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10)); break; default : break; } var date = new Date(year, month, day); return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()); function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;} } }; var validation={ notnull: function($container, $element, content, errormessage) { if (isnull(content)) showerrormessage($container, errormessage); return false; true; }, email: reg=new regexp( ^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$ ); (!reg.test(content)) serialnmuber: new ^[0-9a-za-z_-]*$ number: ^[0-9]*$ humanname: (^[a-z a-z]*$)|(^[\u4e00-\u9fa5]*$) checkbyregexp: regexp, errormessage){ regexp(regexp);>
希望本文所述对大家学习有所帮助。
以上就是Ajax异步方式实现登录与验证的详细内容,更多请关注易知道|edz.cc其它相关文章!