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

柔情痞子 提交于 2019-12-17 20:10:08

问题


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

validatePhoneNumber(sender, args) {
    cleanNumber = args.Value.replace(/\D/, "");
    country = $("#" + CountryID).get(0).value;
    switch (country) {
        case "North America":
            args.IsValid = validateNAPhoneNumber(cleanNumber);
            if (!args.IsValid) sender.errormessage = "* Not a NA Phone #";
            break;
        case "UK":
            args.IsValid = validateUKPhoneNumber(cleanumber);
            if (!args.IsValid) sender.errormessage = "* Not a UK Phone #";
            break;
...
    }
}

The actual validation takes place properly, and the CustomValidator has the correct IsValid property at all times. The sender.errormessage, however, seems to be rewritten just after this function call to it's default value. How can I change the errormessage value, and make it "stick"?


回答1:


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.




回答2:


The best way to change the error message of a validator control with image is:

sender.innerHTML = "YOUR HTML WITH ANYTHING COME HERE"



回答3:


To change the error message, do it like this:

if (!args.IsValid) document.getElementById('<%= cstPhoneNumber.ClientID %>').errormessage = "* Not a NA Phone #";

To change the text, do this:

if (!args.IsValid) document.getElementById('<%= cstPhoneNumber.ClientID %>').innerHTML = "* Not a NA Phone #";

cstPhoneNumber should be replaced by the name of your validation control.




回答4:


It looks like your using jquery so you could set the error message like this:

$(sender).text("* Not a NA Phone #");

Edit:

Sorry for the delay but I was away on vacation.

I was playing a round with a simple page and I think I understand how this works.

When you set the errormessage on the sender in the client side validation function you are setting the equivalent of the ErrorMessage property. This is different from the Text property. The ErrorMessage sets the text displayed in the Validation summary, while the Text property is the text displayed in the validator control. Note that if you only set the ErrorMessage property (on the server) then the ErrorMessage will be copied into the Text property.

What this means is that when you set sender.errormessage your actually setting the text that is displayed in the validation summary.

Hopefully this will help clear it up. However, I still haven't seen a way to change the Text property from the client side validation function. I am also not sure why your sender object is not the validator object.




回答5:


sender.innerText = "Your message" will work.




回答6:


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



回答7:


Just remove all instances of Text and ErrorMessage in the custom validator tag definitions then use

$(#id_of_custom_validation_control).text("custom error message");

Then with this, you can use one custom validator control and handle multiple error conditions

E.g

if(error1){
  $(#id_of_custom_validation_control).text("custom error message1");
}else $(#id_of_custom_validation_control).text("custom error message2");

Hope this helps




回答8:


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.




回答9:


Try this...

Use sender.textContext in the Javascript for the error message and setup your CustomValidator as such:

<asp:CustomValidator ID="cvdPhoneNumber" runat="server" Display="Dynamic" ForeColor="Red" ControlToValidate="txtPhoneNumber" ClientValidationFunction="validatePhoneNumber" ErrorMessage=""  />


来源:https://stackoverflow.com/questions/1230281/how-can-i-rewrite-the-errormessage-for-a-customvalidator-control-on-the-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!