jQuery(document).ready( function() { var parId; /** * 為addForm添加驗證 */ $('#addForm').validate( { errorElement : 'div', errorClass : 'help-block', focusInvalid : false, rules : { name : { required : true }, buildingname : { required : true, min :1 }, area : { required : true, number : true, //正則表達式驗證 doubles: true }, count : { required : true, digits :true, }, remark : { required : true } }, messages : { name : { required : "請填寫名稱" }, remark : { required : "請填寫備注" }, buildingname : { required : "請填寫樓宇名稱", min: "必須樓宇名稱" }, area : { required : "請填寫面積", number : "必須為數字" }, count : { required : "請填寫人數", digits : "必須是整數", } }, highlight : function(e) { $(e).closest('label').removeClass('has-info') .addClass('has-error'); }, success : function(e) { $(e).closest('label').removeClass('has-error'); $(e).remove(); }, errorPlacement : function(error, element) { error.insertAfter(element.parent()); }, submitHandler : function(form) { var name = $('#name').val(); var count = $('#count').val(); var buildingId = $("#buildingname").select2("val"); var area = $('#area').val(); var remark = $('#remark').val(); $.ajax({ url : "roomAjax!addRoom", data : { name : name, count : count, area : area, buildingId : buildingId, remark : remark }, type : "post", dataType : "text", success : function(data) { if (data > 0) { swal({ title : "添加辦公室信息成功!", type : "success" }, function() { $("#room_modal").modal("hide"); $('#room_table').bootstrapTable( 'refresh'); }); } } }); }, invalidHandler : function(form) { } }); $('#save_addForm').on("click", function() { $('#addForm').submit(); }); $('#close_addForm').on("click", function() { $("#room_modal").modal("hide"); }); //正則表達式驗證的寫法 $.validator.addMethod("doubles",function(value,element,params){ var doubles= /^\d+(\.\d{0,2})?$/; return this.optional(element)||(doubles.test(value)); },"小數點后請最多保留兩位小數!"); });
