jq 结合 插件 以remote方式验证 mvc

注意:remote你需要的参数名,如这里的email不能写到data哪里。这是js插件的bug

<script type="text/javascript">
    $(function() {
        $.validator.addMethod("checkName", function(value, element) {
            value = $.trim(value);
            $(element).val(value);
            var reg = /^([\u4e00-\u9fa5]){2,10}$/;
            return this.optional(element) || reg.test(value);
        }, "由汉字2-10字符之间");

 

        $.validator.addMethod("checkPhoneMobile", function(value, element) {
            var mobile = $("[name='mobile']");
            var phone = $("[name='phone']");
            return $.trim(mobile.val()).length > 0 || $.trim(phone.val()).length > 0
        }, "手机和电话至少填一个");

        $.validator.addMethod("imcheck", function(value, element) {
            return this.optional(element) || checkqq(value);
        }, "QQ格式不正确 ");

        $.validator.addMethod("phonecheck", function(value, element) {
            return this.optional(element) || checkPhone(value);
        }, "联系电话格式不正确");

        $.validator.addMethod("mobilecheck", function(value, element) {
            return this.optional(element) || checkMobile(value);
        }, "手机号码格式不正确");

        $.validator.addMethod("postCodecheck", function(value, element) {
            return this.optional(element) || /^\d{5,12}$/.test(value);
        }, "邮政编码格式不正确");

        $('#postMessageForm').validate({
            rules: {
                realName: { checkName: true },
                phone: { phonecheck: true, required: function(ele) { return $.trim($("[name='mobile']").val()).length == 0; } },
                mobile: { mobilecheck: true, required: function(ele) { return $.trim($("[name='phone']").val()).length == 0; } },
                qq: { imcheck: true },
                email: { required: true, email: true, remote: {
                    type: "POST",
                    url: '/CheckOnlyEmail.html',
                    dataType: "json",
                    data: { __RequestVerificationToken: $("[name='__RequestVerificationToken']").val() }
                }
                }
            },
            messages: {
                email: { email: "联系邮箱格式不正确", required: "请联系邮箱", remote: "邮箱已经存在,您可以先<a style='color:#3366CC;' href='http://my.aqioo.com/SignIn.aspx?ReturnUrl=" + encodeURI('http://Pay.aqioo.com/QinSuanConfirm.html') + "'>登录</a>" },
                mobile: { required: "手机和电话至少填一个" },
                phone: { required: "手机和电话至少填一个" }
            },
            errorClass: "error",
            validClass: "valid",

            success: function(label) {
                label.addClass("valid").text("通过");
            },

            errorPlacement: function(error, element) {
                var li = $(element.parent("li"));
                li.find("span.black").hide();
                error.appendTo(li);
                if (li.attr("id") == "phonelis") {
                    li = $("#placed");
                    var reg = /(<span[^>]*class=(?:\"error\"|'error')[^<>]*>手机和电话至少填一个<\/span>)+/ig;
                    li.html(li.html().replace(reg, "$1"));
                }
                else
                    error.appendTo(li);
            },
            errorElement: "span",
            onkeyup: false
        });

        $("#email").blur(function() { $("#postMessageForm").validate().element("#email"); }).attr("keyup", null);
        $("#tijiao").click(function() {
            var form1 = $('#postMessageForm');
            if (form1.valid()) {
                this.disabled = true;
                form1[0].submit();
            }
        });
        $("#tijiao").attr("disabled", false);
    });
</script>

后台

      #region 验证邮箱是否唯一
        [HttpPost]
        public JsonResult CheckEmail(string userEmail,string email)
        {
            userEmail = userEmail.SqlFilter();
            if (string.IsNullOrEmpty(userEmail)) userEmail = email;
            userEmail = userEmail.SqlFilter();
            if (!string.IsNullOrEmpty(userEmail))
            {
                bool result = userService.CheckEmail(userEmail);
                return Json(!result);
            }
            return Json(false);
        }
        #endregion

 

//验证

   return this.optional(element) 起到dependance的作用

<script type="text/javascript" >
            //QQ号的验证
            function checkqq(s) {
                var reg = /^[1-9]\d{4,11}$/;
                return reg.test(s);
            }

 

            //验证号码 可以有空格或者-
            function checkPhone(phone) {
                var reg = /^(([0-9]{3}[-| ]?[0-9]{8})|([0-9]{4}[-| +]?[0-9]{7,8}))$/
                return reg.test(phone);
            }

            //  验证号码
            function checkMobile(s) {
                var reg0 = /^13\d{5,9}$/;
                var reg1 = /^153\d{4,8}$/;
                var reg2 = /^159\d{4,8}$/;
                var reg3 = /^0\d{10,11}$/;
                var my = false;
                if (reg0.test(s)) my = true;
                if (reg1.test(s)) my = true;
                if (reg2.test(s)) my = true;
                if (reg3.test(s)) my = true;
                return my;
            }

            $(function() {
                $.validator.addMethod("checkName", function(value, element) {
                    value = $.trim(value);
                    $(element).val(value);
                    var reg = /^([\u4e00-\u9fa5]){2,10}$/;
                    return this.optional(element) || reg.test(value);
                }, "由汉字2-10字符之间");

                $.validator.addMethod("imcheck", function(value, element) {
                return this.optional(element) || checkqq(value);
                }, "QQ格式不正确 ");

                $.validator.addMethod("phonecheck", function(value, element) {
                return this.optional(element) || checkPhone(value);
                }, "联系电话格式不正确");

                $.validator.addMethod("mobilecheck", function(value, element) {
                return this.optional(element) || checkMobile(value);
                }, "手机号码格式不正确");

                $.validator.addMethod("postCodecheck", function(value, element) {
                return this.optional(element) || /^\d{5,12}$/.test(value);
                }, "邮政编码格式不正确");

                $.validator.addMethod("cRequired", $.validator.methods.required, "请填写此项");
                $.validator.addClassRules("required", { cRequired: true });

                $('#postMessageForm').validate({
                    rules: {
                        realName: { checkName: true },
                        phone: { phonecheck: true },
                        mobile: { mobilecheck: true },
                        qq: { imcheck: true },
                        email: { email: true, required: true },
                        shipRealname: { checkName: "#xxx:checked" },
                        ShipAddress: { required: "#xxx:checked" },
                        shipphone: { required: "#xxx:checked", mobilecheck: "#xxx:checked" },
                        ShippostCode: { required: "#xxx:checked", postCodecheck: "#xxx:checked" }
                    },
                    messages: {
                        email: { email: "联系邮箱格式不正确", required: "请联系邮箱" }
                    },
                    errorClass: "error",
                    validClass: "valid",

                    success: function(label) {
                        label.addClass("valid").text("通过");
                    },

                    errorPlacement: function(error, element) {
                        error.appendTo(element.parents("tr:first").find("td:last"));
                    }

                });

                $("#tiWen").click(function() {
                    if ($('#postMessageForm').valid()) {
                        this.disabled = true;
                    }
                });
            });
           
            function ShowShipInfo(o) {
                if (document.getElementById("xxx").checked) {
                    document.getElementById(o).style.display = "block";
                    document.getElementById("xxx").checked = true;
                }
                else {
                    document.getElementById(o).style.display = "none";
                    document.getElementById("xxx").checked = false;
                }
            }
            // 数字转地支
            function EarthToChi(earth_no) {
                if (earth_no == 1) {
                    return "子";
                } else if (earth_no == 2) {
                    return "丑";
                } else if (earth_no == 3) {
                    return "寅";
                } else if (earth_no == 4) {
                    return "卯";
                } else if (earth_no == 5) {
                    return "辰";
                } else if (earth_no == 6) {
                    return "巳";
                } else if (earth_no == 7) {
                    return "午";
                } else if (earth_no == 8) {
                    return "未";
                } else if (earth_no == 9) {
                    return "申";
                } else if (earth_no == 10) {
                    return "酉";
                } else if (earth_no == 11) {
                    return "戌";
                } else if (earth_no == 12) {
                    return "亥";
                }
            }
</script>

你可能感兴趣的:(remote)