在CompareValidator上添加JS验证

发布于 2024-10-10 11:13:18 字数 64 浏览 0 评论 0原文

是否可以为CompareValidator添加JS验证功能?
我无法使用 CustomValidator。

is it possible to add a JS validation function for CompareValidator?
i can't use a CustomValidator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2024-10-17 11:13:18

我不确定您是否仍然需要它,但我认为不需要,因为这是没有答案的...

好吧,您不能直接执行此操作,但是您可以隐藏负责 CompareValidator 验证的原始函数并引入新的函数。这是可能的,因为所有验证函数都来自全局范围内的 ASP.NET,这是有争议的,但在这种情况下很有帮助。

请找到以下模块来完成工作。它向您展示了两种方法。第一个称为 addFunction 允许您添加一个或多个函数以进行自定义验证。此函数应返回 Boolean 并分别使用三个参数 Validator objectTargetObjectCompareObject 。第二个名为 disableOldFunction 的函数允许您完全摆脱旧的验证函数,如果您的所有函数都有效,则该函数将由模块调用。

    var MyModules = {};
    MyModules.CompareValExtension = function ()
    {
        var functions = new Array();
        var oldFunc = null, disabled = false;

        function myCompareValidatorEvaluateIsValid(val)
        {
            var valid = true;
            if (oldFunc && functions.length)
            {
                for (i in functions)
                {
                    valid = functions[i](val, document.getElementById(val.controltovalidate), document.getElementById(val.controltocompare));
                    if (!valid) { break; }
                }
                if (!disabled && valid)
                {
                    valid = oldFunc(val);
                }
            }
            return valid;
        }

        if (typeof CompareValidatorEvaluateIsValid != 'undefined')
        {
            oldFunc = CompareValidatorEvaluateIsValid;
            window.CompareValidatorEvaluateIsValid = myCompareValidatorEvaluateIsValid;
        }

        var me = {};
        me.addFunction = function (func) { if (typeof func == "function") { functions.push(func); } }
        me.disableOldFunction = function () { disabled = true; }
        return me;
    } ();

用法示例:

    MyModules.CompareValExtension.addFunction(function (val, elem, comp)
    {
        return elem.value == "my value"; 
    });
    MyModules.CompareValExtension.disableOldFunction();

警告:请将此模块放置在页面底部的某个位置,以确保默认验证脚本已就位。您还可以稍微重写模块以推迟初始化,直到文档准备好为止。

I'm not sure whether you still need it or not, I assume not however since this is unanswered...

Well, you can't do it directly, however you can hide original function which is responsible for CompareValidator validation and introduce new one. This is possible because all validation functions comes from ASP.NET in global scope which is... controversial, but helpful in that case.

Please find following module which get the job done. It exposes you two methods. First called addFunction allows you to add one or more functions for custom validation. This function should returns Boolean and it consumes three parameters Validator object , TargetObject and CompareObject respectively. Second one called disableOldFunction allows you to completely get rid of old validation function which is called by the module if all your functions are valid.

    var MyModules = {};
    MyModules.CompareValExtension = function ()
    {
        var functions = new Array();
        var oldFunc = null, disabled = false;

        function myCompareValidatorEvaluateIsValid(val)
        {
            var valid = true;
            if (oldFunc && functions.length)
            {
                for (i in functions)
                {
                    valid = functions[i](val, document.getElementById(val.controltovalidate), document.getElementById(val.controltocompare));
                    if (!valid) { break; }
                }
                if (!disabled && valid)
                {
                    valid = oldFunc(val);
                }
            }
            return valid;
        }

        if (typeof CompareValidatorEvaluateIsValid != 'undefined')
        {
            oldFunc = CompareValidatorEvaluateIsValid;
            window.CompareValidatorEvaluateIsValid = myCompareValidatorEvaluateIsValid;
        }

        var me = {};
        me.addFunction = function (func) { if (typeof func == "function") { functions.push(func); } }
        me.disableOldFunction = function () { disabled = true; }
        return me;
    } ();

Example usage:

    MyModules.CompareValExtension.addFunction(function (val, elem, comp)
    {
        return elem.value == "my value"; 
    });
    MyModules.CompareValExtension.disableOldFunction();

Caveat: Please place this module somewhere on the page bottom to be sure that default validation scripts are in place already. You can also rewrite module a bit to defer initialization until document will be ready.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文