6

我有一个 CustomValidator 正在验证几个不同电话编号方案的电话号码。客户端 javascript 如下所示:

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

实际验证正确进行,并且 CustomValidator 始终具有正确的 IsValid 属性。然而,sender.errormessage 似乎在这个函数调用它的默认值之后被重写了。如何更改 errormessage 值,并使其“粘住”?

4

9 回答 9

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

试试sender.textContent = "your err msg"。它对我有用。

于 2011-11-10T07:43:21.310 回答
3

使用图像更改验证器控件的错误消息的最佳方法是:

sender.innerHTML = "YOUR HTML WITH ANYTHING COME HERE"
于 2012-06-13T04:47:29.767 回答
3

要更改错误消息,请执行以下操作:

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

要更改文本,请执行以下操作:

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

cstPhoneNumber应替换为您的验证控件的名称。

于 2009-11-22T22:40:35.470 回答
2

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.

于 2009-08-05T01:09:42.960 回答
1

sender.innerText = "Your message"将工作。

于 2010-07-28T00:16:42.353 回答
0

我认为问题在于客户端没有“错误消息”属性。自定义验证器呈现一个<span>标签以显示错误消息,其 id 对应于自定义验证器控件的 id 属性。要设置验证失败时显示的文本,您需要执行以下操作。

... 
case "North America":
    args.IsValid = validateNAPhoneNumber(cleanNumber);
    if (!args.IsValid) 
        document.getElementById(sender.id).innerText = "* Not a NA Phone #";
    break;
...
于 2009-08-05T02:15:31.400 回答
0

只需删除自定义验证器标记定义中的所有 Text 和 ErrorMessage 实例,然后使用

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

然后,您可以使用一个自定义验证器控件并处理多个错误条件

例如

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

希望这可以帮助

于 2010-04-21T19:42:21.363 回答
0

使用 jQuery 扩展 @Andy 响应:

更改短信(显示在表格中):

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

更改错误消息(对于摘要控件):

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

替换[Validator]为您的验证者的名称。

于 2013-11-01T15:59:28.017 回答
0

尝试这个...

在 Javascript 中使用sender.textContext获取错误消息并设置您的 CustomValidator,如下所示:

<asp:CustomValidator ID="cvdPhoneNumber" runat="server" Display="Dynamic" ForeColor="Red" ControlToValidate="txtPhoneNumber" ClientValidationFunction="validatePhoneNumber" ErrorMessage=""  />
于 2016-09-23T19:23:03.030 回答