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

时间:2021-01-25 17:15 作者:Mr.Pan 阅读数:267
jQuery(document).ready(
        function() {
            var parId;
            /**
             * 为addForm添加验证
             */
            $('#addForm').validate(
                    {
                        errorElement : 'div',
                        errorClass : 'help-block',
                        focusInvalid : false,
                        rules : {
                            area : {
                                required : true,
                                number : true,
                                //正则表达式验证
                                doubles: true
                            },
                            count : {
                                required : true,
                                digits :true,
                            },
                            remark : {
                                required : true
                            }
                        },
                        messages : {
                            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 area = $('#area').val();
                            var remark = $('#remark').val();
                            $.ajax({
                                url : "roomAjax!addRoom",
                                data : {
                                    area : area,
                                    buildingId : buildingId,
                                    remark : remark
                                },
                                type : "post",
                                dataType : "text",
                                success : function(data) {
                                }
                            });
                        },
                        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));  
            },"小数点后请最多保留两位小数!");  
        });



推荐文章

[推荐] 微信小程序 WXML 模板的使用方法

[推荐] nvm-node版本管理工具

[推荐] Vue自定义指令实现按钮级权限控制功能

[推荐] ant.design 入门踩坑(一)

[推荐] iOS端的UI设计文档

[推荐] vue v-model用法

[推荐] 首个threejs项目-前端填坑指南

[推荐] css 编码三角形

[推荐] postman使用方法

[推荐] dpr 移动web开发之像素和DPR

[推荐] angular的输入和输出的使用方法

[推荐] 前端打包工具 webpack的使用

[推荐] 解决vue组件props传值对象获取不到的问题

[推荐] validate表单验证及正则表达式验证的方法

[推荐] layuiadmintab切换 刷新对应iframe页面 每次都刷新


评论列表



注册用户登录后才能发表评论,请登录注册,访问 博客首页