ASP.NET - Custom Validator with Dynamic ErrorMessage

I'm currently trying to make sure that the number entered in the text box is divisible by 1.25 or 1.5. The way I determine the weather is a number equal to 1.25 or 1.5, depending on what is in the other drop-down list. For example: if the selected DDL index is 1, I mod is 1.5, if it is 2 I mod is 1.25.

However, I need to show the user the cause of the error. The custom validator error message should be something like "The number must be decrypted by 1.25" or vice versa.

From what I can say, the code should work. But this is not so. I read on another forum that takes the source and makes innerText the error message should do the trick. But I have to do something wrong. When I go through my javascript function, it goes fine. Just an 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>
+3
source share
2 answers

There were a few minor bugs in your javascript functions depending on case sensitivity (fe IsValidand Value). I debugged it to find out which Error-Span property I should set. This was an attribute textContentfor Firefox and innerTextfor IE.

Work function (cross browser):

  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;
                }
        }
    }
+5
source

, . , :

"" ( , firefox 20), , , , , . "args" - .

+4

Source: https://habr.com/ru/post/1783506/


All Articles