通用设置
var CC={};
CC.validate = {
// focusCleanup: false,
// focusInvalid: false,
onclick: function() {
return false;
},
onfocusin: function() {
return false;
},
onfocusout: function() {
return false;
},
onkeyup: function() {
return false;
},
}
//添加校验规则
$.validator.addMethod("mobile", function(value, element, params) {
return /^1[34578]\d{9}$/.test(value);
});
//添加校验规则,有且仅有数字、字母
$.validator.addMethod("password", function(password, element, params) {
var iNow = 0;
if (password.match(/\s/)) {
return false;
}
if (password.match(/[\u4E00-\u9FA5]/g)) {
return false;
}
if(password.match(/[0-9]/g)){
iNow++;
}
if(password.match(/[a-z]/ig)){
iNow++;
}
if(password.match(/[~!@#$%^&*]/g)){
iNow++;
}
console.log(inow);
if(iNow < 2){
return false;
}
return true;
});
使用
function showErrorMsg(msg) {
$('#errorDispaly').show().find('p').html(msg);
}
function clearErrorMsg() {
$('#errorDispaly').hide().find('p').html(null);
}
$("#registerForm").validate($.extend(true, CC.validate, {
rules: {
mobilePhone: {//input的id
required: true,//自带校验规则
mobile: true//使用校验规则
},
password: {//input的id
required: true,//自带校验规则
rangelength:[6, 20],//自带校验规则
password: true,//使用校验规则
},
smSCaptcha: {//input的id
required: true,//自带校验规则
minlength: 6,//自带校验规则
maxlength: 6,//自带校验规则
}
},
messages: {
mobilePhone: {
required: '请输入手机号码',
mobile: '手机号输入有误'
},
password: {
required: '请输入新密码',
rangelength:'密码为6-20位,至少含数字、字母及特殊符号中的2种',
password: '密码为6-20位,至少含数字、字母及特殊符号中的2种'
},
smSCaptcha: {
required: '请输入手机验证码',
minlength: '短信验证码输入有误',
maxlength: '短信验证码输入有误',
}
},
showErrors: function(errMap, errList) {
//对错误信息集中处理
if (errList && errList.length) {
showErrorMsg(errList[0].message);
} else {
clearErrorMsg();
}
},
submitHandler:function(form){
if ($('#registerBtn').hasClass('btn-disabled')) return;
$('#submitBtn').addClass('disabled');
$.post("test.php", {
mobilePhone: $('#mobile').val(),
password: $('#passphrase').val(),
mobileCaptcha: $('#mobileCaptcha').val(),
}, function(res) {
if (res.success) {
window.location.replace(document.referrer || '/');
} else {
$('#registerBtn').removeClass('btn-disabled');
showErrorMsg('error');
}
});
return;
}
}));
$('#registerBtn').on('touchstart, click', function(e) {
e.preventDefault();
$('#registerForm').submit();
})