bootstrap validator 使用笔记
准备工作 校验类型客户端前台自校验 <div class="form-group">
<label class="col-lg-3 control-label">Full name</label>
<div class="col-lg-4">
<input type="text" class="form-control" name="firstName" placeholder="First name" required data-bv-notempty-message="firstName不能为空(提示语)" />
</div>
</div>
校验常用方式 data-bv-notempty-message="XXX不能为空"
data-bv-stringlength="true" data-bv-stringlength-min="6" data-bv-stringlength-max="30" data-bv-stringlength-message="这个字段长度不得小于6,不得超过30 "
type="email" data-bv-emailaddress-message="The input is not a valid email address"
data-bv-date="false" data-bv-date-format="YYYY/MM/DD" data-bv-date-message="The birthday is not valid"
前后台交互校验 $('#defaultForm').bootstrapValidator({
message: 'This value is not valid',feedbackIcons: { //提示图标
valid: 'glyphicon glyphicon-ok',invalid: 'glyphicon glyphicon-remove',validating: 'glyphicon glyphicon-refresh'
},fields: {
firstName: {
validators: {
notEmpty: { // 非空校验+提示信息
message: 'The first name is required and cannot be empty'
}
}
},lastName: {
validators: {
notEmpty: {
message: 'The last name is required and cannot be empty'
}
}
},username: {
message: 'The username is not valid',validators: {
notEmpty: {
message: 'The username is required and cannot be empty'
},stringLength: { //输入 长度限制 校验
min: 6,max: 30,message: 'The username must be more than 6 and less than 30 characters long'
},regexp: { //正则校验
regexp: /^[a-zA-Z0-9_.]+$/,message: 'The username can only consist of alphabetical,number,dot and underscore'
},remote: {
url: 'remote.php',message: 'The username is not available'
},different: {
field: 'password',message: 'The username and password cannot be the same as each other'
}
}
},email: {
validators: {
emailAddress: { // 邮箱格式校验
message: 'The input is not a valid email address'
}
}
},password: {
validators: {
notEmpty: {
message: 'The password is required and cannot be empty'
},identical: {
field: 'confirmPassword',message: 'The password and its confirm are not the same'
},different: {
field: 'username',message: 'The password cannot be the same as username'
}
}
},captcha: {
validators: {
callback: { //提交到后台进行校验
message: 'Wrong answer',//提示信息
callback: function(value,validator) {
//用ajax提交到后台,进行校验。如果校验失败 return false; 校验成功 return true;
var items = $('#captchaOperation').html().split(' '),sum = parseInt(items[0]) + parseInt(items[2]);
return value == sum;
}
}
}
}
}
});
表单提交前校验 $('#defaultForm').bootstrapValidator({
message: 'This value is not valid',feedbackIcons: {
valid: 'glyphicon glyphicon-ok',fields: {
……
……
}
})
.on('success.form.bv',function(e) {
// Prevent form submission
e.preventDefault();
// Get the form instance
var $form = $(e.target);
// Get the BootstrapValidator instance
var bv = $form.data('bootstrapValidator');
// Use Ajax to submit form data
$.post($form.attr('action'),$form.serialize(),function(result) {
console.log(result);
},'json');
});
总结? ? ? ?对于成熟的框架来说,都会有很方便的写法,这样才会发挥出框架的作用。我们要做的就是学会使用,快速的开发。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |