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

发布于 2024-10-10 05:34:32 字数 1489 浏览 2 评论 0原文

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

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

据我所知,代码应该可以工作。但事实并非如此。我在另一个论坛上读到,获取源代码并将 insideText 设置为错误消息应该可以解决问题。但我一定是在某个地方做错了什么。当我单步执行 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>

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.

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.

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:

<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>

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

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

发布评论

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

评论(2

梅倚清风 2024-10-17 05:34:32

根据区分大小写的情况,您的 JavaScript 函数中存在一些小错误(fe IsValidValue)。我已经对其进行了调试,以查看我必须设置 Error-Span 的哪些属性。对于 Firefox,它是 textContent 属性;对于 IE,它是 innerText 属性。

工作功能(跨浏览器):

  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;
                }
        }
    }

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

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;
                }
        }
    }
只是我以为 2024-10-17 05:34:32

抱歉,作为访客无法评论其他答案。我想指出这个问题以及蒂姆斯的回答的一个问题:

使用“参数”作为参数名称可能会导致麻烦(至少我刚刚在 Firefox 20 中经历过),因为它也是一个自动变量,包含传递给函数的所有参数,因此如果参数的命名方式相同,则会出现名称冲突。我建议将参数名称更改为“args”或其他任何名称。

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:

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.

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