jquery validate自定义验证

首先导包:




 

js中添加 手机自定义验证

jQuery.validator.addMethod("isMobile", function(value, element) {
    var length = value.length;
    var mobile = /^1[34578]\d{9}$/;/*/^1(3|4|5|7|8)\d{9}$/*/
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "请正确填写您的手机号码");



$("#saveBtn").click(function() {
    
          $("#mechantForm").validate({
              submitHandler: function(form){  
                  $.ajax({
                        cache : true,
                        type : "POST",
                        url : "merchant",
                        data : $('#mechantForm').serialize(),
                        async : false,
                        error : function(data) {
                            layer.alert('系统错误', {icon: 0});
                        },
                        success : function(data) {
                            layer.alert('保存成功', {icon: 1});
                            $('#mechantForm')[0].reset();
                        }
                    });
                     },
                rules: {
                    merchantName: {
                        required: true,
                         minlength: 3
                    },
                    name:{
                        required: true,
                         minlength: 2
                    },
                    telphone:{
                        required: true,
                        minlength: 11,
                        maxlength:11,
                        digits:true,
                        number:true,
                        isMobile : true
                    },
                    address:{
                        required: true,
                        maxlength:50
                    }
                },
                messages: {
                    merchantName: {
                        required: "不能为空",
                         minlength: "不能少于3个字符"
                    },
                    name:{
                        required: "不能为空",
                         minlength: "不能少于2个字符"
                    },
                    telphone:{
                        required: "不能为空",
                        minlength: "必须11位数字",
                        maxlength:"必须11位数字",
                        digits:"必须是数字"    ,
                        number:"请输入有效数字",
                        isMobile : "手机号格式错误"
                    },
                    address:{
                        required:'不能为空',
                        maxlength:"不能多于50个字符"
                    }
                }
            });
});

本文转自博客园
@ Java野生程序猿

你可能感兴趣的:(jquery validate自定义验证)