Bootstrapvalidator校正、校正清除重設的實現代碼(推薦),bootstrapvalidator

來源:互聯網
上載者:User

Bootstrapvalidator校正、校正清除重設的實現代碼(推薦),bootstrapvalidator

1.引入css與js

  bootstrapValidator.min.css

  bootstrapValidator.min.js

2.html中的modal代碼

<div class="modal fade" id="editModal" 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="editModalLabel"></h4></div><div class="modal-body"><!-- form start --><form class="form-horizontal" id="saveadmin_form"name="form-horizontal"><input type="hidden" id="adminid" /><div class="box-body"><dl class="dl-horizontal"><div class="form-group"><div class="col-sm-10"><dt>管理員名</dt><dd><lable id="edit_show_adminname"></lable><input type="text" class="form-control" id="edit_adminName"name="edit_adminName" data-bv-notempty="true"name="edit_adminName"></dd></div></div><div class="form-group" id="div_password"><div class="col-sm-10"><dt>密碼</dt><dd><input type="text" class="form-control" name="edit_passwd"id="edit_passwd"></dd></div></div><div class="form-group" id="div_password1"><div class="col-sm-10"><dt>確認密碼</dt><dd><input type="text" class="form-control" name="edit_passwd1"id="edit_passwd1"></dd></div></div><div class="form-group"><div class="col-sm-10"><dt>顯示名</dt><dd><input type="text" name="edit_displayName"class="form-control" id="edit_displayName"></dd></div></div><div class="form-group"><div class="col-sm-10"><dt>郵箱</dt><dd><input type="text" data-bv-notempty="true" name="edit_Mail"class="form-control" id="edit_Mail"></dd></div></div><div class="form-group"><div class="col-sm-10"><dt>備忘</dt><dd><textarea class="form-control" name="edit_desc" rows="3"id="edit_desc"></textarea></dd></div></div></dl></div><!-- /.box-body --><div class="modal-footer"><!-- <button type="button" onclick="saveAdmin()" class="btn btn-default">儲存</button> --><button onclick="saveAdmin()" type="button" class="btn btn-success"><i class="fa fa-check"></i> 儲存</button><button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times"></i> 關閉</button></div></form></div></div></div></div>

3.js代碼

//儲存function saveAdmin(){//開啟驗證$('#saveadmin_form').data('bootstrapValidator').validate(); if(!$('#saveadmin_form').data('bootstrapValidator').isValid()){ return ; } //表單提交$.ajax({type: "POST",dataType : 'json',url: "<%=request.getContextPath()%>/user/saveUser.html?ma="+Math.random(),data: {"type" :"0","id":$("#adminid").val(),"account":$("#edit_adminName").val(),"display_name":$("#edit_displayName").val(),"password":$("#edit_passwd").val(),"mail":$("#edit_Mail").val(),"role":$("#edit_role").val(),"desc":$("#edit_desc").val()},success :function(json) {json = eval("("+json+")");$("#editModal").modal("hide");$("#dialog_content").html(json.message);$("#dialog_button_queren").hide();$("#dialog_modal").modal("show");t.ajax.reload( null, true );}});}//初始化表單驗證$(document).ready(function() {formValidator();});/*********************************校正重設重點在這裡 當modal隱藏時銷毀驗證再重新載入驗證****************************************/ //Modal驗證銷毀重構$('#editModal').on('hidden.bs.modal', function() {$("#saveadmin_form").data('bootstrapValidator').destroy();$('#saveadmin_form').data('bootstrapValidator', null);formValidator();});//form驗證規則function formValidator(){$('#saveadmin_form').bootstrapValidator({message: 'This value is not valid',feedbackIcons: {valid: 'glyphicon glyphicon-ok',invalid: 'glyphicon glyphicon-remove',validating: 'glyphicon glyphicon-refresh'},fields: {//管理員名edit_adminName: {message: '管理員名驗證失敗',validators: {notEmpty: {message: '管理員名不可為空'},stringLength: {min: 5,max: 64,message: '管理員名長度必須在6到64位之間'}}},//密碼edit_passwd: {message: '密碼驗證失敗',validators: {notEmpty: {message: '密碼不可為空'},stringLength: {min: 5,max: 64,message: '密碼長度在5到64之間'}/* ,identical: {field: 'edit_passwd1',message: '兩次密碼不相同'} */}},//密碼確認edit_passwd1: {message: '密碼確認驗證失敗',validators: {notEmpty: {message: '密碼確認不可為空'},identical: {field: 'edit_passwd',message: '兩次密碼不相同'}}},//顯示名edit_displayName: {message: '使用者名稱驗證失敗',validators: {notEmpty: {message: '顯示名不可為空'},stringLength: {min: 5,max: 128,message: '顯示名長度必須在6到18位之間'}}},//郵箱edit_Mail: {validators: {notEmpty: {message: '郵箱不可為空'},emailAddress: {message: '郵箱格式正確'},stringLength: {max:256,message: '郵箱長度必須小於256'}}},//備忘edit_desc: {message: '備忘驗證失敗',validators: {stringLength: {max: 256,message: '備忘長度長度必須小於256'}}},}});}

以上所述是小編給大家介紹的Bootstrapvalidator校正、校正清除重設的實現代碼(推薦),希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.