• jquery validate 自定义验证方法(前后台交互)


    JS:

    var dataCode = {
        key: "IsHasAUserName",
        AUserName: function() {

            return $("#AUserName").val();
        }
    };
    var remoteHasAUserNameFun= GetRemoteInfo('/Service/CommanHandler.ashx', dataCode);

    $("#form1").validate({
            rules: {
                txtUserName: {
                    required: true,
                    remote: remoteHasAUserNameFun
                },
                txtPass: {
                    required: true,
                    minlength: 6
                },
                txtConfirmPass: {
                    required: true,
                    equalTo: "#txtPass"
                },
                txtName: {
                    required: true
                }
            },
            messages: {
                txtUserName: {
                    required: "请输入账号"
                },
                txtPass: {
                    required: "请输入新密码",
                    minlength: "密码最少不得少于6位字符"
                },
                txtConfirmPass: {
                    required: "请输入确认密码",
                    equalTo: "密码不一致。"
                },
                txtName: {
                    required: "请输入姓名"
                }
            },
            errorLabelContainer: $(".bg-info")
        });

    C#:

    private XmlDocument IsHasAUserName()
            {
                string AUserName = contextWai.Request.Form["AUserName"];
    
                AjaxClass ajaxClass = new AjaxClass();
                commonHelperManager manager = commonHelperManager.GetInstrance();
                object result = manager.ExecuteScalar(sql, CommandType.Text, Field.SetParam("@AUserName",AUserName));
                if (Convert.ToInt32(result)>0)
                {
                    ajaxClass.Msg = "账号已存在,请另换一个。";
                    ajaxClass.Result = 0;
                }
                else
                {
                    ajaxClass.Msg = "验证成功。";
                    ajaxClass.Result = 1;
                }
                string resultxml = Utility.Tool.Serialize(ajaxClass);
    
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(resultxml);
                contextWai.Response.ContentType = "text/xml"; //must be 'text/xml'
                contextWai.Response.ContentEncoding = System.Text.Encoding.UTF8; //we'd like UTF-8
                doc.Save(contextWai.Response.Output); //save to the text-writer
                return doc;
            }

    另外一种:

    //验证
    function validateFrom() {
        jQuery.validator.addMethod("CheckPinYin", function (value, element) {
            if ($.trim(value) != "") {
                var result = "0";
    
                $.ajax({
                    url: '/SystemPage/Service/News/NewsClassHandler.ashx',
                    type: 'POST',
                    async: false,//同步,在这里不能用异步的,因为validate是即时验证的
                    data: {
                        "key": "CheckPinYinOfSonClass",
                        "CPinYin": value,
                        "ClassId": classID,
                        "PageURL": "/SystemPage/News/AddNewsClass.aspx"
                    },
                    dataType: 'text',
                    timeout: 8000,
                    success: function (e) {
                        result = e;//不能讲判断放在这里,放在外面才可以判断,可能在$.ajax里return true or false表示的意思冲突了。
                    }
                });
                if (result == "1") {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }, "此标识已存在,请另换一个");
    
        $("#form1").validate({
            rules: {
                txtCName: {
                    required: true
                },
                txtCByName:
                {
                    required: true
                }, txtCPinYin: {
                    required: true,
                    CheckPinYin: true
                }, txtCNewsTemplateUrl: {
                    required: true
                }, txtClassTemplet:
                {
                    required: true
                }
                , txtNewsTemplet:
                {
                    required: true
                }
            },
            messages: {
                txtCName: {
                    required: "请输入……"
                }, txtCByName:
                {
                    required: "请输入……"
                }, txtCPinYin: {
                    required: "请输入……"
                }, txtCNewsTemplateUrl: {
                    required: "请输入……"
                }, txtClassTemplet:
                {
                    required: "请选择……"
                }
                , txtNewsTemplet:
                {
                    required: "请选择……"
                }
            }
            , success: function (label) {
                label.html(" ").attr("class", "success").siblings("label").remove();
            },
            errorPlacement: function (error, element) {
                $(element).next("span").find(".success").remove();
                error.appendTo(element.next("span"));
            }
        });
    }
  • 相关阅读:
    Git Cannot rebase: You have unstaged changes.
    importError: DLL load failed when import matplotlib.pyplot as plt
    install tushare in python 3.6
    pd.qcut, pd.cut, df.groupby()等在分组和聚合方面的应用
    从池子里的beta看秋香, 个性迥异
    个股和股票池的beta系数的估算
    检验两个随机序列的beta系数
    spyder里的"查找文件里的特定字符串"非常方便
    地图上道路编号中的G S X Y
    场内的代码表, 感觉水很深
  • 原文地址:https://www.cnblogs.com/hougelou/p/4194308.html
Copyright © 2020-2023  润新知