简体   繁体   English

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

[英]ASP.NET Custom Validator Client side & Server Side validation not firing

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:我查看了 output 和 ASP.NET 正在识别客户端 function:

ASP.NET JavaScript output: ASP.NET JavaScript output:

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编辑:错字我贴错了function,问题还是一样

Just another update to the last comment: where by the TextBox cannot be empty.只是对最后一条评论的另一个更新:TextBox 不能为空。 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 触发了我的客户端验证 function 很好,没有值:

<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" />

Use this:用这个:

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

To validate an empty field.验证一个空字段。

You don't need to add 2 validators !您不需要添加 2 个验证器!

Your CustomValidator will only fire when the TextBox isn't empty.您的CustomValidator只会在TextBox不为空时触发。

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

Client-side validation was not being executed at all on my web form and I had no idea why.我的 web 表单上根本没有执行客户端验证,我不知道为什么。 It turns out the problem was the name of the javascript function was the same as the server control ID.原来问题是 javascript function 的名称与服务器控件 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 冲突?

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?您是否验证了导致回发的控件已将 CausesValidation 设置为 tru 并且没有为其分配验证组?

I'm not sure what else might cause this behavior.我不确定还有什么可能导致这种行为。

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还要检查您是否没有使用验证组,因为如果设置了验证组属性并且未通过显式调用验证,则验证不会触发

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

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

(as you wrote) (正如你所写)

Thanks for that info on the ControlToValidate LukeH!感谢您提供有关 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.我在我的代码中尝试做的是仅确保当文本字段 B 具有特定值时某些文本字段 A 在字段中具有一些文本。 Otherwise, A can be blank or whatever else.否则, A 可以为空白或其他任何内容。 Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.去掉我标记中的 ControlToValidate="A" 为我解决了这个问题。

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM