1. 程式人生 > >bootstrapValidator驗證表單後清除當次驗證的方法

bootstrapValidator驗證表單後清除當次驗證的方法

 用bootstrapValidator的resetForm()方法:

<!-- // create server begin -->
<div class="modal fade" id="server-create-server-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog modal-create"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="clearValidate('server-create-server-modal-form')"> × </button> <h4 id="headDeptLable" class="modal-title">建立儲存伺服器 </h4> </div> <form id="server-create-server-modal-form" method="post" class="form-horizontal" action=""> <div class="modal-body"> <!-- // modal-body begin --> <div class="form-group"> <label class="col-lg-4 col-md-4 col-sm-4 control-label font-normal align-right">名稱:</label> <div class="col-lg-5 col-md-5 col-sm-5"> <input type="text" class="form-control" id="server-create-server-modal-name" name="serverCreateServerModalName" placeholder="" /> </div> </div> <div class="form-group"> <label class="col-lg-4 col-md-4 col-sm-4 control-label font-normal align-right">型別:</label> <div class="col-lg-5 col-md-5 col-sm-5"> <select id="server-create-server-modal-type" name="serverCreateServerModalType" class="form-control"> <option value="">請選擇</option> <option value="AU">儲存密碼機</option> <option value="CA">伺服器二</option> <option value="FR">伺服器三</option> <option value="DE">伺服器四</option> <option value="IT">伺服器五</option> <option value="JP">伺服器六</option> <option value="GB">伺服器七</option> <option value="US">伺服器八</option> <option value="VN">伺服器九</option> </select> </div> </div> <div class="form-group"> <label class="col-lg-4 col-md-4 col-sm-4 control-label font-normal align-right">IP:</label> <div class="col-lg-5 col-md-5 col-sm-5"> <input type="text" class="form-control" id="server-create-server-modal-ip" name="serverCreateServerModalIp" /> </div> </div> <!-- // modal-body end --> </div> <div class="modal-footer"> <button class="btn btn-success" id="server-create-server-modal-submit" type="submit"> 確 定 </button> <button class="btn btn-default" type="button" data-dismiss="modal" onclick="clearValidate('server-create-server-modal-form')"> 取 消 </button> </div> </form> </div><!-- /Modal --> </div> </div><!-- // create server end --> 

 

    $('#server-create-server-modal-form')
    .bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { serverCreateServerModalName: { message: '名稱無效', validators: { notEmpty: { message: '名稱不能為空' }, stringLength: { min: 1, max: 30, message: '名稱只能為1至30個字元' }, regexp: { regexp: /^[a-zA-Z0-9_\.]+$/, message: '名稱只能為數字字母下劃線' } } }, serverCreateServerModalType: { message: '型別無效', validators: { notEmpty: { message: '型別不能為空' } } }, serverCreateServerModalIp: { message: 'IP碼無效', validators: { notEmpty: { message: 'IP不能為空' }, regexp: { regexp: /^(\d|\d\d|1\d\d|2[0-4]\d|25[0-5])(?:\.(\d|\d\d|1\d\d|2[0-4]\d|25[0-5])){3}$/g, message: '請輸入正確的IP' } } } } }) .on('success.form.bv', function(e) { e.preventDefault();//取消submit預設提交效果 $("#server-create-server-modal-form")[0].reset();//重置表單,此處用jquery獲取Dom節點時一定要加[0] $("#server-create-server-modal-form").data('bootstrapValidator').resetForm();//清除當前驗證的關鍵之處 $("#server-create-server-modal").modal("hide"); toastr.success("建立伺服器成功", "成功提示"); $serverTable.bootstrapTable("refresh"); });