web端核心代码
$(document).ready(function() {
$('#defaultForm').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
entry_code: {
validators: {
notEmpty: {
message: '验证码不能为空'
},
stringLength: {
min: 4,
max: 4,
message: '验证码长度为4位',
},
remote: {
url: CONTROLLER+'/ajax_check_code',
message:"验证码不正确",
type: "get",
dataType: 'json',
data: {
},
delay: 500,
},
}
},
}
});
})
服务器与js之间的交互协议是json,不像网上所说的字符串“”true“和”false”.
如果用TP验证,调用的方法结果类似如下:
$code = I('entry_code');
if(check_verify($code)) {
$this->ajaxReturn(array('valid'=>true,'message'=>'验证码正确'));
} else {
$this->ajaxReturn(array('valid'=>false,'message'=>'验证码不正确'));
}
xhr.then(function(response) {
dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true', response.message ? response.message : null);
});