javascript ASP.NET - 带有动态错误消息的自定义验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4598882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:16:10  来源:igfitidea点击:

ASP.NET - Custom Validator with Dynamic ErrorMessage

javascriptasp.netcustom-validators

提问by Johnrad

I am currently trying to make sure a number entered in a textbox is divisibly by 1.25 or 1.5. The wayI decide weather to mod the number by 1.25 or 1.5 is dependant on what is in another drop down list. For example: If the selected index of a DDL is 1, I mod by 1.5, if it is 2 I mod by 1.25.

我目前正在尝试确保在文本框中输入的数字可以被 1.25 或 1.5 整除。我决定天气将数字修改为 1.25 或 1.5 的方式取决于另一个下拉列表中的内容。例如:如果选择的 DDL 索引是 1,则 I mod by 1.5,如果是 2 I mod by 1.25。

However I need to display to the user why the error was thrown. The error message of the custom validator needs to be something like "Number Must be devisible by 1.25" or vice versa.

但是我需要向用户显示抛出错误的原因。自定义验证器的错误消息需要类似于“Number Must be devisible by 1.25”,反之亦然。

From what I can tell me code should work. But it doesn't. I read on another forum that taking the source and making the innerText your error message should do the trick. But I must be doing something wrong somewhere. When I step through my javascript function it steps through perfectly. Just no error message. Here is my code:

据我所知,代码应该可以工作。但事实并非如此。我在另一个论坛上读到,获取源代码并使innerText 成为您的错误消息应该可以解决问题。但我一定是在某个地方做错了什么。当我逐步执行我的 javascript 函数时,它会完美地执行。只是没有错误信息。这是我的代码:

<asp:CustomValidator ID="ValidateFinHeight" runat="server" CssClass="NormLabel" 
Display="Dynamic" 
ControlToValidate="txtFinHeight" 
ClientValidationFunction="validateFinHeight"></asp:CustomValidator>

<script type="text/javascript" language="javascript" >
function validateFinHeight(source, arguments)
{
  var ddl = document.getElementById('cboTubeDia');
  var ddlSelIndex = ddl.selectedIndex

  switch(ddlSelIndex)
  {
    case 0:
        arguments.isValid = true;
        return;     
    case 1:
        if(arguments.value%1.25 != 0)
        {
            source.innerText = "Height must be divisibly by 1.25";
            arguments.isValid = false;
            return;
        }
        else
        {
            arguments.isValid = true;
            return;
        }
    case 2:
        if(arguments.value%1.5 != 0)
        {
            source.innerText = "Height must be divisibly by 1.5";
            arguments.isValid = false;
            return;
        }
        else 
        {
            arguments.isValid = true;
            return;
        } 
  }
}
</script>

回答by Tim Schmelter

There were several minor mistakes in your javascript functions according to case sensitivity(f.e. IsValidand Value). I have debugged it to see what property of the Error-Span i must set. It was the textContentattribute for Firefox and innerTextfor IE.

根据区分大小写(feIsValidValue),您的 javascript 函数中有几个小错误。我已经调试它以查看我必须设置的 Error-Span 的哪些属性。它是textContentFirefox 和innerTextIE的属性。

The working function(cross browser capable):

工作功能(支持跨浏览器):

  function validateFinHeight(source, args) {
        var ddl = document.getElementById('cboTubeDia');
        var ddlSelIndex = ddl.selectedIndex;
        var errorMsg = "";

        switch (ddlSelIndex) {
            case 0:
                args.IsValid = true;
                return;
            case 1:
                if (args.Value % 1.25 != 0) {
                    errorMsg = "Height must be divisibly by 1.25";
                    if (source.innerText) {
                        source.innerText = errorMsg;
                    } else {
                        source.textContent = errorMsg;
                    }
                    args.IsValid = false;
                    return;
                }
                else {
                    args.IsValid = true;
                    return;
                }
            case 2:
                if (args.Value % 1.5 != 0) {
                    errorMsg = "Height must be divisibly by 1.5";
                    if (source.innerText) {
                        source.innerText = errorMsg;
                    } else {
                        source.textContent = errorMsg;
                    }
                    args.IsValid = false;
                    return;
                }
                else {
                    args.IsValid = true;
                    return;
                }
        }
    }

回答by Matthias

Sorry, can't comment other answers as a guest. I'd like to point out an issue with the Question as well as Tims answer:

抱歉,不能以访客身份评论其他答案。我想指出问题以及 Tims 回答的一个问题:

Using 'arguments' as a parameter name may lead to trouble (at least I just experienced that in firefox 20) because it is also an auto-variable which contains all arguments that are passed to the function, so there is a name conflict if a parameter is named the same way. I recommend changing the parameter name to 'args' or anything else.

使用“arguments”作为参数名可能会导致麻烦(至少我刚刚在 firefox 20 中遇到过这种情况),因为它也是一个包含传递给函数的所有参数的自动变量,因此如果 a参数的命名方式相同。我建议将参数名称更改为 'args' 或其他任何名称。