導入相關的 js,css
<link rel="stylesheet" href="<%=path%>/assets/plugins/bootstrap-3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="<%=path%>/assets/plugins/bootstrapvalidator-0.5.2/css/bootstrapValidator.css" />
<!--bootstrap dialog 用於彈出框-->
<link rel="stylesheet" href="<%=path%>/assets/plugins/bootstrap3-dialog/css/bootstrap-dialog.min.css" />
<script src="<%=path%>/assets/js/jquery-1.12.3.min.js">
<script src="<%=path%>/assets/plugins/jquery-bootpag/jquery.bootpag.min.js"></script>
<script src="<%=path%>/assets/plugins/bootstrapvalidator-0.5.2/js/bootstrapValidator.min.js"></script>
<!--bootstrap dialog 用於彈出框-->
<script src="<%=path%>/assets/plugins/bootstrap3-dialog/js/bootstrap-dialog.min.js"></script>
jsp的相關代碼
</div>
<button type="button" class="btn btn-sm yellow" id="add">添加(S)</button>
</div>
<!-- 模態框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×
</button>
<h4 class="modal-title" id="myModalLabel">
添加
</h4>
</div>
<div class="modal-body">
<form action="" method="post" class="form-horizontal" id="defaultForm">
<div class="form-group">
<label class="col-lg-3 control-label">UserId</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="userId" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">賬號</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="username" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">密碼</label>
<div class="col-lg-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
<div class="form-group">
<div class="col-lg-9 col-lg-offset-3">
<button type="submit" class="btn btn-primary">Sign up</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">關閉
</button>
<button type="button" class="btn btn-primary">
提交保存
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
js代碼
放在$(function(){})
裏面
//彈出定義的 模態框(Modal)
$("#add").bind("click", function(Event) {
$('#myModal').modal({
keyboard: true
});
});
//表單驗證bootstrap validation form-group的div是必須的
$('#defaultForm')
.bootstrapValidator({
message: '值不能爲空',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
userId: {
message: 'The username is not valid',
validators: {
notEmpty: {//非空驗證
message: 'id必填且不能爲空'
},
stringLength: {//長度驗證
min: 3,
max: 30,
message: '長度必須在3-30之間'
},
remote: {//與後臺校驗驗證 bootstrap的remote驗證器需要的返回結果一定是json格式的數據 :{"valid":false} //表示不合法,驗證不通過 {"valid":true} //表示合法,驗證通過
url: '<%=path%>/validatorController/validator',
message: '用戶id已存在',
delay : 500,//每輸入一個字符,就發ajax請求,服務器壓力還是太大,設置0.5秒發送一次ajax(默認輸入一個字符,提交一次,服務器壓力太大)
type: 'POST'//請求方式
/**自定義提交數據,默認值提交當前input value
* data: function(validator) {
return {
password: $('[name="passwordNameAttributeInYourForm"]').val(),
whatever: $('[name="whateverNameAttributeInYourForm"]').val()
};
}
*/
},
regexp: {//自定義規則
regexp: /^[a-zA-Z0-9_\.]+$/,
message: '用戶名只能由字母、數字、點和下劃線組成 '
}
}
},
username: {
validators: {
notEmpty: {//非空驗證:提示消息
message: '名稱必填且不能爲空'
},
identical: {//相同
field: 'userId', //需要進行比較的input name值
message: 'id與用戶名應該保持一致'
}
}
},
password: {
validators: {
notEmpty: {
message: '密碼不能爲空'
},
stringLength: {
min: 5,
max: 20,
message: '長度必須在6-20之間'
},
}
}
}
})
.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');
});
注意 input 必須要使用 form-group的div是必須的
即 <div class="form-group"></div> 是必要的,不然沒效果
<div class="form-group">
<label class="col-lg-3 control-label">密碼</label>
<div class="col-lg-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
http://www.cnblogs.com/franson-2016/p/5613696.html
http://www.cnblogs.com/xiaoruilin/p/5807922.html