How can I rewrite the ErrorMessage for a CustomValidator control on the client?

前端 未结 9 1306
鱼传尺愫
鱼传尺愫 2020-12-11 05:45

I have a CustomValidator that is validating a telephone number for several different telephone numbering schemes. The client-side javascript looks like this:



        
相关标签:
9条回答
  • 2020-12-11 06:09

    I think the problem is that there is no 'errormessage' property on the client side. The custom validator renders a <span> tag to display the error message with an id corresponding the id property of the custom validator control. To set the text displayed when validation fails you need to do somthing like this.

    ... 
    case "North America":
        args.IsValid = validateNAPhoneNumber(cleanNumber);
        if (!args.IsValid) 
            document.getElementById(sender.id).innerText = "* Not a NA Phone #";
        break;
    ...
    
    0 讨论(0)
  • 2020-12-11 06:15
    function dateofbirth(sender, args) {
    
        var dtcDOB = document.getElementById('<%= dtcDOB.ClientID %>');
    
        var dob = new Date(dtcDOB.value);
        var currDate = new Date();
    
        if (dtcDOB.value == "") {
            args.IsValid = false;
            sender.textContent = "Provide DOB.";
            return;
        }
    
        args.IsValid = true;
    }
    

    Try sender.textContent = "your err msg". It worked for me.

    0 讨论(0)
  • 2020-12-11 06:18

    Expanding @Andy response, using jQuery:

    Change the text message (show in the form):

    $('#<%=this.[Validator].ClientID%>').text('Text to show');
    

    Change the error message (for the Summary control):

    $('#<%=this.[Validator].ClientID%>').attr('errormessage', 'Text to show');
    

    Replace [Validator] for your validator's name.

    0 讨论(0)
提交回复
热议问题