jquery validate验证方法

摄郎Elements 2014-04-10

jQuery.validator.addMethod("stringCheck", function(value, element) {
        // return this.optional(element) || /^[0-9a-zA-Z_]{1,}$/.test(value);
        return this.optional(element) || /^[a-zA-Z]+[a-zA-Z0-9]+$/.test(value);
    }, "只能由英文字母或数字组成(不支持中文、不能以数字开头)");
    //  }, "只能包括英文字母、数字和下划线");
    jQuery.validator.addMethod("mobile", function (value, element, param) {       
        var pattern = /^\d+$/;
        return this.optional(element) || pattern.test(value);
    }, '只能输入数字');
    //表单验证
    $('#registerbox').validate({
        rules: {
            UserName: {
                required: true,
                stringCheck: true,
                minlength: "3",
                maxlength: "15",
               
                cache: false,
                remote: {
                    type: 'get',
                    url: "/Account/Validate",
                    dataType: 'json',
                    data: {
                        str: function () {
                            return $("#username").val();
                        }
                    }
                },
                onkeyup: false
            },
            Password: {
                required: true,
            },
            Pwd_s: {
                required: true,
                equalTo: "#pass"
            },
            Email: {
                required: true,
                email: true,
                cache: false,
                remote: {
                    type: 'get',
                    url: "/Account/Validate",
                    dataType: 'json',
                    data: {
                        str: function () {
                            return $("#userEmail").val();
                        }
                    }
                }
            },
            MemberType: {
                required: true,
            },
            Mobile: {
                required: true,               
                minlength: "11",
                maxlength: "11",              
                mobile: true,
                cache: false,
                number:true,
                remote: {
                    type: 'get',
                    url: "/Account/Validate",
                    dataType: 'json',
                    data: {
                        str: function () {
                            return $("#userPhone").val();
                        }
                    }
                }
            },
            rule: {
                required: true
            }
        },
        messages: {
            UserName: {
                required: "用户名不能为空",
                stringCheck: "只能由英文字母或数字组成(不支持中文、不能以数字开头)",
                minlength: "请输入3-15个字节(一个中文字算2个字节)",
                maxlength: "请输入3-15个字节(一个中文字算2个字节)",
               
                remote:"该用户名已被注册"
            },
            Password: {
                required: "密码不能为空",
            },
            Pwd_s: {
                required: "确认密码不能为空",
                equalTo: "两次密码不一样"
            },
            MemberType: {
                required: "选择用户类型",
            },
            Email: {
                required: "Email不能为空",
                email: "请输入正确的Email地址",
                remote: "该邮箱已被注册"
            },
            Mobile: {
                required: "手机不能为空",               
                minlength: "请输入11位手机号码",
                maxlength: "请输入11位手机号码",               
                mobile: "只能输入数字",
                remote: "该手机已被注册"
            },
            rule: "未接受条款"
        }
    });

相关推荐

Web全栈笔记 / 0评论 2020-06-15