首页 > 代码库 > jQuery validation 去除空格

jQuery validation 去除空格

技术分享

当input输入了空格是不会提示信息的

一般会去除空格然后进行验证

这个时候就要添加onkeyup事件去除左侧的空格

 //添加验证手机方法
        jQuery.validator.addMethod("isMobile", function(value, element) {
            var length = value.length;
            var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");
        //表单验证
        $("#enterpriseAuth").validate({
            debug: true,
            onkeyup: function(element, event) {
                //去除左侧空格
                var value = http://www.mamicode.com/this.elementValue(element).replace(/^/s+/g,"");
                $(element).val(value);
            },
            errorClass: ‘errMsg‘,
            errorElement: "div",
            errorPlacement: function(error, element) {
                error.appendTo(element.parent());
            },
            groups: {
                distpicker: "province city district"
            },
            rules: {
                enterpriseName: {
                    required: true
                },
                managerPhone: "isMobile",
                managerEmail: {
                    email: true,
                },
                province: "required",
                city: "required",
                district: "required"
            },

            messages: {
                enterpriseName: {
                    required: "请填写企业名称",
                },
                enterpriseAddress: {
                    required: "请填写公司注册地址",
                },
                enterpriseContact: {
                    required: "请填写街道详细地址",
                },
                managerName: {
                    required: "请填写管理者姓名",
                },
                managerEmail: {
                    required: "请填写管理者邮箱",
                    email: "请填写正确邮箱格式"

                },
                managerPhone: {
                    required: "请输入手机号",
                    isMobile: "请填写正确手机号"
                },

                province: {
                    required: "请选择省市区",

                },
                city: {
                    required: "请选择省市区",

                },
                district: {
                    required: "请选择省市区",
                }
            }
        });

  

jQuery validation 去除空格