标签:
<div class="email_reg m_part">
<form class="reg_form" id="reg_form">
<p>邮箱注册</p>
<div class="control">
<label>邮箱</label><input class="email" name="email">
<span>请填写本人常用邮箱,该邮箱将作为登录账号</span>
</div>
<div class="control">
<label>密码</label><input type="password" name="password">
<span>字母、数字或者英文符号,6-20位</span>
</div>
<div class="control">
<label>确认密码</label><input type="password" name="confirmpwd">
<span>请再次输入密码</span>
</div>
<input class="submit" id="submit_btn" onclick="return false;" type="submit" value="注册">
</form>
</div>
<script>
$("#reg_form").validate({
ok:function(){
alert("登录啦!");
}
});
</script>
(function(jQuery){
"use strict";
(function(factor){
if(jQuery && !jQuery.fn.validate){
factor($);
}
})(function($){
var flagAll = {
email:false,
password:false,
samePwd:false
};
var methods = {
settings:null,
init:function(item){
methods.settings = $.extend(true,{},$.fn.validate.defaults,item);
return this.each(function(){
var cur = $(this);
var checkItem = cur.find("input[name]");
methods.subCheck(cur);
methods.checkItem(checkItem);
});
},
subCheck:function(box){
var btn = box.find("input[type=‘submit‘]");
btn.on("click",function(){
methods.allCheck(box);
});
},
allCheck:function(box){
var checkItem = box.find("input[name]");
$.each(checkItem,function(i,v){
var cur = $(v);
methods.blueE(cur);
methods.checkFmt(cur);
});
if(flagAll.email && flagAll.password && flagAll.samePwd){
alert("submit");
}
},
checkItem:function(item){
$.each(item,function(){
var cur = $(this);
methods.checkSingle(cur);
});
},
checkSingle:function(item){
item.on("blur",function(){
methods.blueE(item);
methods.checkFmt(item);
});
item.keydown(function(e){
var code = e.keyCode;
if(code == 13){
methods.allCheck(item);
}
}).keyup(function(e){
methods.blueE(item);
});
},
blueE:function(item){
var val = item.val();
if(val == ""){
var txt = methods.getType(item);
methods.errorInfo(item,txt);
}else{
var error = item.next(".error");
error.length > 0 ? error.remove() : ‘‘;
//methods.checkFmt(item); 边输入变验证
}
},
getType:function(item){
var type = item.attr("name");
var txt = "";
switch (type){
case "email":
txt = methods.settings.emailTxt;
break;
case "password":
txt = methods.settings.pwdTxt;
break;
case "confirmpwd":
txt = methods.settings.confirmTxt;
break;
}
return txt;
},
checkFmt:function(item){
var val = item.val();
var type = item.attr("name");
switch (type){
case "email":
methods.checkEmailFmt(item,val);
break;
case "password":
methods.checkPwdFmt(item,val);
break;
case "confirmpwd":
methods.checkPwdFmt(item,val,true);
}
},
checkEmailFmt:function(item,val){
var reg,info;
reg =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.com)$/;
info = "邮箱地址不正确,请重新输入";
var flag = reg.test(val);
if(flag){
flagAll.email= true;
}else{
methods.errorInfo(item,info);
flagAll.email = false;
}
},
checkPwdFmt:function(item,val,state){
var reg,info;
reg = /^[-,.?:;‘"~!`#%<>=_\@\*\$\^\&\(\)\{\}\[\]\/ | 0-9 |A-Z |a-z]{6,20}$/;
info = "请输入6-20位合理字符";
var flag = reg.test(val);
if(flag){
if(state){
methods.checkDoublePwd(val,item);
}else{
methods.checkSafe(val,item);
flagAll.password= true;
}
}else{
methods.errorInfo(item,info);
state ? flagAll.samePwd = false : flagAll.password = false;
}
},
checkDoublePwd:function(val,item){
var pwd = $("input[name=‘password‘]").val();
if(pwd != val){
flagAll.samePwd = false;
var txt = "两次密码不一致,请重新输入";
methods.errorInfo(item,txt);
}else{
flagAll.samePwd = true;
}
},
checkSafe:function(val,item){
var Modes;
for (var i=0;i<val.length;i++){
//密码模式
Modes|=methods.CharMode(val.charCodeAt(i));
}
var level = methods.bitTotal(Modes);
methods.showLevel(level,item);
},
showLevel:function(level,item){
switch (level){
case 1:
var txt = "密码设置太简单,安全性弱";
methods.errorInfo(item,txt,"error");
break;
case 2:
var txt = "密码安全性中";
methods.errorInfo(item,txt,"middlePwd");
break;
default :
var txt = "密码安全性强";
methods.errorInfo(item,txt,"strongPwd");
}
},
CharMode:function(iN){
if (iN>=48 && iN <=57) //数字
return 1;
if (iN>=65 && iN <=90) //大写
return 2;
if (iN>=97 && iN <=122) //小写
return 4;
else
return 8;
},
bitTotal:function(num){
var modes=0;
for (var i=0;i<6;i++){
if (num & 1) modes++;
num>>>=1;
}
return modes;
},
/*checkSafe:function(val,item){
var reg1 = /^\d+$/,
reg2 = /^[a-zA-Z]$/,
reg3 = /^(\d*)([a-zA-Z]{1,2})(\d*)$/,
reg4 = /^([a-zA-Z]*)(\d{1,2})([a-zA-Z]*)$/;
var regm = /^([a-zA-Z0-9]*)([-,.?:;‘"~!`#%<>=_\@\*\$\^\&\(\)\{\}\[\]\/]{1,2})([a-zA-Z0-9]*)$/;
if((reg1.test(val) || reg2.test(val) || reg3.test(val) || reg4.test(val)) && val.length == 6){
var txt = "密码设置太简单,安全性弱";
methods.errorInfo(item,txt,"error");
}else if(regm.test(val)){
var txt = "密码安全性中";
methods.errorInfo(item,txt,"middlePwd");
}else{
var txt = "密码安全性强";
methods.errorInfo(item,txt,"strongPwd");
}
},*/
errorInfo:function(item,txt,cls){
var scls = cls || "error";
var div = "<span class=‘validate_cls "+scls+"‘>"+txt+"</span>";
if(item.next(".validate_cls").length == 0){
$(div).insertAfter(item);
}else{
var box = item.next(".validate_cls");
box.html(txt).removeClass().addClass("validate_cls").addClass(scls);
}
}
};
$.fn.validate = function(param){
return methods.init.apply(this,param);
};
$.fn.validate.defaults = {
emailTxt:"请输入邮箱",
pwdTxt:"请输入密码",
confirmTxt:"请确认密码"
};
});
})(jQuery);
标签:
原文地址:http://www.cnblogs.com/lr-blog/p/5465505.html