var form = $('#form-test');
$(document).ready(function () {
form.bootstrapValidator({
live: 'enabled',
excluded: [':disabled', ':hidden', ':not(:visible)'],
submitButtons: '#btn-test',//指定提交按钮,如果验证失败则变成disabled,但我没试成功,反而加了这句话非submit按钮也会提交到action指定页面
message: '通用的验证失败消息',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
CUST_NAME: {
validators: {
notEmpty: {//检测非空,radio也可用
message: '请输入用户名'
},
stringLength: {//检测长度
min: 2,
max: 10,
},
regexp: {//正则验证
regexp: /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/,
message: '所输入的字符不符要求'
},
}
},
CONTACT_MAIL: {
validators: {
emailAddress: {//验证email地址
message: '请输入正确的邮件地址如:[email protected]'
},
notEmpty: {//检测非空
message: '请输入邮箱'
},
}
},
CONTACT_TEL:{
validators:{
notEmpty:{
message:'手机号码不能为空'
},
stringlength:{
min:11,
max:11,
message:'请输入11位手机号码'
},
regexp:{
regexp:/^1[3|5|6|7|8|9]{1}[0-9]{9}$/,
message:'请输入正确的手机号码'
}
}
},
MSG_SUB:{
validators:{
notEmpty:{
message:'标题不能为空'
},
}
},
MSG_CONTENT:{
validators:{
notEmpty:{
message:'请填写内容'
}
}
}
},
}) ;
});
//弹窗提示
function showToast(msg,shortCutFunction)
{
toastr.options = {
"closeButton": true,
"debug": false,
"progressBar": true,
"positionClass": "toast-bottom-right",
"onclick": null,
"showDuration": "400",
"hideDuration": "1000",
"timeOut": "7000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr[shortCutFunction](msg,"提示");
}
$("#btn-test").click(function () {
var bv = form.data('bootstrapValidator');
bv.validate();
if(bv.isValid()) {
$.ajax({
url:url地址,
type:'post',
data:form.serialize(),
dataType:'jsonp',
jsonp:'callback',
success:function(data){
window.location.href = '../index.html';
},error:function(){
showToast("测试中的错误","error");
}
})
}
});
// 特别提示 跨域 提交数据会提交2条 处理条件 让后台处理跨域问题