$(function () {
var form = $('form');
form.bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
username: {
message: '用户名验证失败',
validators: {
notEmpty: {
message: '用户名不能为空'
},
stringLength: {
min: 6,
max: 18,
message: '用户名长度必须在6到18位之间'
},
regexp: {
regexp: /^[a-zA-Z0-9_]+$/,
message: '用户名只能包含大写、小写、数字和下划线'
},
threshold : 6 , //用户名大于等于6位才发ajax
remote: { // ajax校验,获得一个json数据({'valid': true or false})
message: '用户已存在', //提示信息
url: '/register', //验证地址
type: 'POST', //请求方式
delay : 2000//每输入一个字符,就发ajax请求,服务器压力还是太大,设置2秒发送一次ajax(默认输入一个字符,提交一次,服务器压力太大)
// data: function(validator){ //自定义提交数据,默认为当前input name值
// return {
// act: 'is_registered',
// username: $("input[name='username']").val()
// };
// }
}
}
},
email: {
validators: {
notEmpty: {
message: '邮箱不能为空'
},
emailAddress: {
message: '请输入正确的邮箱地址 如:47125@qq.com'
}
}
},
password: {
message:'密码无效',
validators: {
notEmpty: {
message: '密码不能为空'
},
stringLength: {
min: 6,
max: 30,
message: '密码长度必须在6到30之间'
},
different: {//不能和用户名相同
field: 'username',//需要进行比较的input name值
message: '密码不能和用户名相同'
}
}
},
repassword: {
message: '密码无效',
validators: {
notEmpty: {
message: '请确认密码'
},
stringLength: {
min: 6,
max: 30,
message: '密码长度必须在6到30之间'
},
identical: {//相同
field: 'password',
message: '两次密码不一致'
}
}
}
}
}).on('success.form.bv', function(e) {
e.preventDefault();
});
});