一、前言
BootstrapValidator 是基於 Bootstrap 3 的 jQuery 表單驗證外掛,也是與 Bootstrap 框架結合最為契合的表單驗證外掛。工作中用到這個框架,便順手把累積的一些心得記錄下來。
二、問題描述
當按鈕的 type 為 submit 時,使用 bootstrapValidator 的 isValid() 能讓表單驗證正常運作;但當 button 的 type 為 button 時,只呼叫 isValid() 卻無法正常觸發驗證。這時候就需要改用 bootstrapValidator 的 validate() 方法來「主動激活」驗證流程。
三、專案程式碼
1、JSP 中
<span style="font-size:14px;"><div id="addAdminDialog" class="modal fade" tabindex="-1"> <div class="modal-dialog cy-modal-dialog-f"> <div class="modal-content"> <div class="modal-header"> <button class="close" data-dismiss="modal"><span>×</span></button> <h4 class="modal-title" id="myLargeModalLabel">新增管理員</h4> </div> <div class="modal-body"> <form id="addAdminForm" method="post" action="${ctx}/admin/operator/add.htm" class="form-horizontal"> <div class="form-group"> <label class="form-label text-bold" for="">登錄名</label> <input class="form-control" placeholder="" type="text" name="loginName" id="addLoginName"/> </div> <div class="form-group"> <label class="form-label text-bold" for="">用戶名</label> <input class="form-control" placeholder="" type="text" name="userName" id="addUserName" /> </div> <div class="form-group"> <label class="form-label text-bold" for="">密碼</label> <input class="form-control" placeholder="" type="password" name="password" id="addPassword" /> </div> <div class="form-group"> <label class="form-label text-bold" for="">角色</label> <div class="form-group"> <div class="col-md-10 col-xs-10"> <div class="checkbox cy-nopadding" id="addRoles"> <c:forEach var="role" items="${roles}"> <label name="addRoleLabel" class="form-checkbox form-icon form-text"><input type="checkbox" id="addRole_${role.id}" name="roles" value="${role.id}"> ${role.roleName}</label> </c:forEach> </div> </div> </div> </div> </form> </div> <!--Modal footer--> <div class="modal-footer"> <button class="btn btn-primary" onclick="addAdmin();">確定</button> <button data-dismiss="modal" class="btn btn-default" type="button">取消</button> </div> </div> </div> </div></span> 2、JS
<span style="font-size:14px;">var faIcon = { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' } //新增管理員前台校驗 $("#addAdminForm").bootstrapValidator({ message: 'This value is not valid', //反饋圖標 feedbackIcons:faIcon, fields: { loginName:{ message:'登錄名無效', validators:{ notEmpty:{ message:'登錄名不能為空' }, StringLength:{ min:5, max:30, message:'用戶名長度大於6位並且小於30位' }, regexp:{ regexp:/^[a-zA-Z0-9_]+$/, message:'用戶名只能由字母、數字和下劃線' } } }, userName: { message: '用戶名格式不正確', validators: { notEmpty: { message: '用戶名不能為空' }, stringLength: { min: 6, max: 30, message: '用戶名長度大於6位並且小於30位' }, regexp: { regexp: /^[a-zA-Z0-9_]+$/, message: '用戶名只能由字母、數字和下劃線' } } }, password: { validators: { notEmpty: { message: '郵箱不能為空' }, emailAddress: { message: '輸入的不是一個有效的電子郵件地址' } } } } });</span> <span style="font-size:14px;">// 新增操作員 function addAdmin() { /*手動驗證表單,當是普通按鈕時。*/ $('#addAdminForm').data('bootstrapValidator').validate(); if(!$('#addAdminForm').data('bootstrapValidator').isValid()){ return ; } $("#addAdminForm").ajaxSubmit({ dataType : 'json', type : "post", success : function(json) { if (json.status == "succ") { doQuery(); Modal.alert({ msg : "操作成功" }); $("#addAdminDialog").modal('hide'); } else { Modal.alert({ msg : json.msg }); } }, error : function() { Modal.alert({ msg : "操作失敗" }); } }); }</span> 四、效果圖


這個問題的本質,在於按鈕類型的不同會影響瀏覽器是否自動觸發表單提交行為。當 type="submit" 時,點擊按鈕會觸發表單的提交事件,驗證器得以攔截並執行校驗;而 type="button" 只是普通按鈕,不會觸發提交,因此僅呼叫 isValid() 只是「查詢」當前狀態,並不會真正跑一次驗證。解法就是先呼叫 validate() 主動啟動校驗,再用 isValid() 讀取結果——順序不能顛倒,這也是前面文章曾經提過的同一個重點。