validate表单验证及正则表达式验证的方法

时间:2022-03-27 19:45:19
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));  
            },"小数点后请最多保留两位小数!");  
        });