/**
* 验证器
*/
var validator = {
// 所有验证规则
types: {},
// 错误信息存放
messages: [],
// 验证方法
validate: function(validateData, validateTypes) {
// 清空所有的错误信息
this.messages = [];
for (var type in validateTypes) {
if (this.types.hasOwnProperty(type) && !this.types[type].validate(validateData, validateTypes[type])) {
this.messages.push(this.types[type].message());
};
}
},
// 判断是否有错误
hasErrors: function() {
return this.messages.length !== 0;
}
};
validator.types.notNull = {
validate: function(value) {
return value !== "";
},
message: function() {
return "不能为空";
}
};
validator.types.minLength = {
minLen: 8,
validate: function(value, minLen) {
if(minLen){
this.minLen = minLen;
}
return value.length >= minLen;
},
message: function() {
return "长度不能少于" + this.minLen;
}
};
validator.types.maxLength = {
maxLen: 40,
validate: function(value, maxLen) {
if (maxLen) {
this.maxLen = maxLen;
}
return value.length <= maxLen;
},
message: function() {
return "长度不能超过" + this.maxLen;
}
};
validator.types.isEmail = {
validate: function(value) {
// var re_email = new RegExp("^([a-zA-Z0-9]+[_|\_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$");
var re_email = /^(\w+(?:[-+.]\w+)*@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*)$/i;
return re_email.test(value);
},
message: function() {
return "邮箱不合法";
}
};
validator.types.notRegister = {
validate: function(value) {
// 没有错误信息的时候才AJAX请求
if(!validator.messages.length) {
var res = false;
var url = membersConfig['passportUrl']+"/index.php?r=UserX/verifyname";
var client_id = $("#client_id").val();
// 调用验证用户名是否被注册请求
$.ajax({
url: url,
type: "post",
async: false,
data: { username: value,client_id:client_id },
dataType: 'json',
success: function(result){
if (result.result == "true"){
res = true;
}
}
});
return res;
} else {
return true;
}
},
message: function() {
return "邮箱已被注册";
}
};
validator.types.pwdWeak = {
/**
* 检查密码强度
* 字符集:数字,字母,标点
* 空:返回0,长度==0
* 弱:返回1,长度<8 || 只有一种字符
* 中:返回2,长度>=8 || 有两种字符
* 强:返回3,长度>=8 || 三种字符
* @param value
* @return 0|1|2|3
*/
checkPass: function (value){
var result = 0;
if(value.length > 0 && value.length < 8) return ++result;
/\d/g.test(value) && ++result;
/[a-zA-Z]/g.test(value) && ++result;
/\W|_/g.test(value) && ++result;
return result;
},
validate: function (value) {
return !(this.checkPass(value) == 1);
},
message: function () {
return "长度必须为8-20位,且为字母,数字和标点符号中的2种";
}
};
validator.types.isPass = {
validate: function(value) {
return value[0] === value[1];
},
message: function() {
return "两次密码不一致";
}
};
validator.types.isTel = {
validate: function(value) {
var tel=/(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
return tel.test(value);
},
message: function() {
return "非法电话号码";
}
};
validator.types.hasRead = {
validate: function(value) {
return $("#read").prop("checked")==true?true:false;
//return value === true;
},
message: function() {
return "请阅读注册条款";
}
};
validator.types.isTelExist = {
validate: function(value) {
var client_id = $("#client_id").val();
if(!validator.messages.length){
var res = false;
var url = membersConfig['passportUrl']+"/index.php?r=index/istelexist";
// 调用验证手机号是否已验证过
$.ajax({
url: url,
type: 'get',
async: false,
data: { tel: value ,client_id:client_id },
dataType: 'json',
success: function(result){
if(result.result == "true"){
res = true;
}
}
});
return res;
}else{
return true;
}
},
message: function(){
return "号码已被使用";
}
};
validator.types.idnumber = {
validate: function(value) {
// must input 17 bit string of RID from left to right
function calcChecksum(rid) {
var arr = rid.split('').reverse();
function W(i) {
return Math.pow(2, i - 1) % 11;
}
function S() {
var sum = 0;
for (var j = 0; j < 17; j++) {
sum += arr[j] * W(j + 2);
}
return sum;
}
var ret = (12 - (S() % 11)) % 11;
return ret === 10 ? 'X' : ret;
}
return value.length === 18 && calcChecksum(value.substr(0, 17)) == value[17];
},
message: function() {
return "无效身份证号码";
}
};
var configs = {
tel: {
notNull: true,
isTel: true
},
tel_check: {
notNull: true
}
};
/**
* input框光标离开事件
*/
$(".validate").blur(function(){
var value = $(this).val();
var id = $(this).attr('id');
var config = id && configs[id] || null;
validator.validate(value, config);
if (validator.hasErrors()) {
$(this).css('border-color', '#f00');
$("#"+id+"_success").hide();
$("#"+id+"_error").text(validator.messages[0]).show();
} else {
$(this).css('border-color', '#b5b5b5');
$("#"+id+"_error").hide();
$("#"+id+"_success").show();
};
// 手机验证
(id === "tel") && $("#tel_fetch").attr("disabled", validator.hasErrors());
});