ASP。NET自定义验证器客户端和服务器端验证不触发

时间:2021-11-16 16:33:54

This has not happened to me before, but for some reason both the client and server side validation events are not being triggered:

我以前没有遇到过这种情况,但是出于某种原因,客户端和服务器端验证事件都没有被触发:

<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
    ErrorMessage="Delivery Town or City required"
    ClientValidationFunction="TextBoxDTownCityClient" 
    ControlToValidate="TextBoxDTownCity"
    OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>

Server-side validation event:

服务器端验证事件:

protected void TextBoxDTownCity_Validate(object source, ServerValidateEventArgs args)
{
    args.IsValid = false;
}

Client-side validation event:

客户端验证事件:

function TextBoxDCountyClient(sender, args) {
    args.IsValid = false;
    alert("test");
}

I thought at the least the Server Side validation would fire but no. this has never happened to me before. This has really got me stumped.

我认为服务器端验证至少会启动,但不会。这在我之前从未发生过。这真让我为难。

I looked at the output and ASP.NET is recognizing the client side function:

我查看了输出和ASP。NET是识别客户端功能:

ASP.NET JavaScript output:

ASP。净JavaScript输出:

var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");

ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";

ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";

ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";

ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";

ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";

Rendered custom validator:

呈现自定义验证器:

<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span> 

Can any one shed some light as to why both client and server side validation would not be firing.

对于为什么客户端和服务器端验证都不会被触发,谁能给出一些解释呢?

Edit: Typo I pasted in the wrong function, problem still the same

编辑:我粘贴错了功能,问题还是一样

Just another update to the last comment: where by the TextBox cannot be empty. I tested this out and it is not true. On a blank page the CustomValidator fired my client side validation function fine without a value:

最后一个注释的另一个更新:文本框的位置不能为空。我测试了一下,这不是真的。在一个空白页上,CustomValidator触发了我的客户端验证功能,没有值:

<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

7 个解决方案

#1


108  

Your CustomValidator will only fire when the TextBox isn't empty.

您的CustomValidator只有在文本框不是空的时候才会触发。

If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.

如果您需要确保它不是空的,那么您也需要一个RequiredFieldValidator。

Note: If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

注意:如果输入控件为空,则不调用验证函数,验证成功。使用RequiredFieldValidator控件要求用户在输入控件中输入数据。

EDIT:

编辑:

If your CustomValidator specifies the ControlToValidate attribute (and your original example does) then your validation functions will only be called when the control isn't empty.

如果您的CustomValidator指定了ControlToValidate属性(您的原始示例也是),那么只有当控件不是空的时候,才会调用验证函数。

If you don't specify ControlToValidate then your validation functions will be called every time.

如果不指定ControlToValidate,那么每次都会调用验证函数。

This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator, you could omit the ControlToValidate attribute from the CustomValidator and setup your validation functions to do something like this:

这为这个问题提供了第二个可能的解决方案。而不是使用单独的RequiredFieldValidator,您可以省略CustomValidator中的ControlToValidate属性,并设置验证函数来完成以下操作:

Client Side code (Javascript):

客户端代码(Javascript):

function TextBoxDCountyClient(sender, args) {
    var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
    if (v == '') {
        args.IsValid = false;  // field is empty
    }
    else {
        // do your other validation tests here...
    }
}

Server side code (C#):

服务器端代码(c#):

protected void TextBoxDTownCity_Validate(
    object source, ServerValidateEventArgs args)
{
    string v = TextBoxDTownCity.Text;
    if (v == string.Empty)
    {
        args.IsValid = false;  // field is empty
    }
    else
    {
        // do your other validation tests here...
    }
}

#2


118  

Use this:

用这个:

<asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>

To validate an empty field.

验证一个空字段。

You don't need to add 2 validators !

您不需要添加两个验证器!

#3


3  

Did you verify that the control causing the post back has CausesValidation set to tru and that it does not have a validation group assigned to it?

您是否验证了导致post返回的控件已经将CausesValidation设置为tru,并且它没有分配给它的验证组?

I'm not sure what else might cause this behavior.

我不知道还有什么能引起这种行为。

#4


3  

Client-side validation was not being executed at all on my web form and I had no idea why. It turns out the problem was the name of the javascript function was the same as the server control ID.

在我的web表单上根本没有执行客户端验证,我不知道为什么。事实证明,问题是javascript函数的名称与服务器控件ID相同。

So you can't do this...

所以你不能这么做…

<script>
  function vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />

But this works:

但是这个工作原理:

<script>
  function validate_vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />

I'm guessing it conflicts with internal .NET Javascript?

我猜它与。net Javascript内部冲突?

#5


2  

Also check that you are not using validation groups as that validation wouldnt fire if the validationgroup property was set and not explicitly called via

还要检查是否没有使用验证组,因为如果设置了validationgroup属性,并且没有显式地通过via调用,那么验证不会触发

 Page.Validate({Insert validation group name here});

#6


0  

Server-side validation won't fire if client-side validation is invalid, the postback is not send.

如果客户端验证无效,则不会触发服务器端验证,也不会发送回发。

Don't you have some other validation that doesn't pass?

难道你没有其他的不合格的证明吗?

The client-side validation is not executed because you specified ClientValidationFunction="TextBoxDTownCityClient" and this will look for a function named TextBoxDTownCityClient as validation function, but the function name should be TextBoxDAddress1Client

客户端验证没有执行,因为您指定了ClientValidationFunction=“TextBoxDTownCityClient”,这将查找名为TextBoxDTownCityClient的函数作为验证函数,但是函数名应该是TextBoxDAddress1Client

(as you wrote)

(你写)

#7


0  

Thanks for that info on the ControlToValidate LukeH!

谢谢控制验证卢克!

What I was trying to do in my code was to only ensure that some text field A has some text in the field when text field B has a particular value. Otherwise, A can be blank or whatever else. Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.

我在代码中要做的是确保当文本字段B有特定值时,文本字段A在字段中有一些文本。否则,A可以是空的。去掉标记中的control ="A"为我解决了这个问题。

Cheers!

干杯!

#1


108  

Your CustomValidator will only fire when the TextBox isn't empty.

您的CustomValidator只有在文本框不是空的时候才会触发。

If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.

如果您需要确保它不是空的,那么您也需要一个RequiredFieldValidator。

Note: If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

注意:如果输入控件为空,则不调用验证函数,验证成功。使用RequiredFieldValidator控件要求用户在输入控件中输入数据。

EDIT:

编辑:

If your CustomValidator specifies the ControlToValidate attribute (and your original example does) then your validation functions will only be called when the control isn't empty.

如果您的CustomValidator指定了ControlToValidate属性(您的原始示例也是),那么只有当控件不是空的时候,才会调用验证函数。

If you don't specify ControlToValidate then your validation functions will be called every time.

如果不指定ControlToValidate,那么每次都会调用验证函数。

This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator, you could omit the ControlToValidate attribute from the CustomValidator and setup your validation functions to do something like this:

这为这个问题提供了第二个可能的解决方案。而不是使用单独的RequiredFieldValidator,您可以省略CustomValidator中的ControlToValidate属性,并设置验证函数来完成以下操作:

Client Side code (Javascript):

客户端代码(Javascript):

function TextBoxDCountyClient(sender, args) {
    var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
    if (v == '') {
        args.IsValid = false;  // field is empty
    }
    else {
        // do your other validation tests here...
    }
}

Server side code (C#):

服务器端代码(c#):

protected void TextBoxDTownCity_Validate(
    object source, ServerValidateEventArgs args)
{
    string v = TextBoxDTownCity.Text;
    if (v == string.Empty)
    {
        args.IsValid = false;  // field is empty
    }
    else
    {
        // do your other validation tests here...
    }
}

#2


118  

Use this:

用这个:

<asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>

To validate an empty field.

验证一个空字段。

You don't need to add 2 validators !

您不需要添加两个验证器!

#3


3  

Did you verify that the control causing the post back has CausesValidation set to tru and that it does not have a validation group assigned to it?

您是否验证了导致post返回的控件已经将CausesValidation设置为tru,并且它没有分配给它的验证组?

I'm not sure what else might cause this behavior.

我不知道还有什么能引起这种行为。

#4


3  

Client-side validation was not being executed at all on my web form and I had no idea why. It turns out the problem was the name of the javascript function was the same as the server control ID.

在我的web表单上根本没有执行客户端验证,我不知道为什么。事实证明,问题是javascript函数的名称与服务器控件ID相同。

So you can't do this...

所以你不能这么做…

<script>
  function vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />

But this works:

但是这个工作原理:

<script>
  function validate_vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />

I'm guessing it conflicts with internal .NET Javascript?

我猜它与。net Javascript内部冲突?

#5


2  

Also check that you are not using validation groups as that validation wouldnt fire if the validationgroup property was set and not explicitly called via

还要检查是否没有使用验证组,因为如果设置了validationgroup属性,并且没有显式地通过via调用,那么验证不会触发

 Page.Validate({Insert validation group name here});

#6


0  

Server-side validation won't fire if client-side validation is invalid, the postback is not send.

如果客户端验证无效,则不会触发服务器端验证,也不会发送回发。

Don't you have some other validation that doesn't pass?

难道你没有其他的不合格的证明吗?

The client-side validation is not executed because you specified ClientValidationFunction="TextBoxDTownCityClient" and this will look for a function named TextBoxDTownCityClient as validation function, but the function name should be TextBoxDAddress1Client

客户端验证没有执行,因为您指定了ClientValidationFunction=“TextBoxDTownCityClient”,这将查找名为TextBoxDTownCityClient的函数作为验证函数,但是函数名应该是TextBoxDAddress1Client

(as you wrote)

(你写)

#7


0  

Thanks for that info on the ControlToValidate LukeH!

谢谢控制验证卢克!

What I was trying to do in my code was to only ensure that some text field A has some text in the field when text field B has a particular value. Otherwise, A can be blank or whatever else. Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.

我在代码中要做的是确保当文本字段B有特定值时,文本字段A在字段中有一些文本。否则,A可以是空的。去掉标记中的control ="A"为我解决了这个问题。

Cheers!

干杯!