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

发布于 2024-07-16 07:25:35 字数 2327 浏览 4 评论 0原文

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

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

服务器端验证事件:

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

客户端验证事件:

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

我认为至少服务器端验证会触发,但没有。 我以前从未发生过这种情况。 这实在是让我难住了。

我查看了输出,ASP.NET 正在识别客户端功能:

ASP.NET 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";

渲染的自定义验证器:

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

任何人都可以阐明为什么客户端和服务器端验证都不会被触发。

编辑:拼写错误,我粘贴了错误的函数,问题仍然相同

只是对最后一条评论的另一个更新:文本框不能为空。 我对此进行了测试,但事实并非如此。 在空白页面上,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" />

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

能怎样 2024-07-23 07:25:35

使用此:

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

验证空字段。

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

Use this:

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

To validate an empty field.

You don't need to add 2 validators !

弥枳 2024-07-23 07:25:35

仅当 TextBox 不为空时,您的 CustomValidator 才会触发。

如果您需要确保它不为空,那么您需要 RequiredFieldValidator 也是如此。

注意: 如果输入控件为空,
没有调用验证函数并且
验证成功。 用一个
requiredFieldValidator 控件
要求用户在其中输入数据
输入控制。

编辑:

如果您的 CustomValidator 指定 ControlToValidate 属性(并且您的原始示例也是如此),那么您的验证函数将仅当控件不为空时被调用。

如果您没有指定ControlToValidate,那么每次都会调用您的验证函数。

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

:代码 (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...
    }
}

服务器端代码 (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...
    }
}

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

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

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.

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.

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

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:

Client Side code (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#):

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...
    }
}
野の 2024-07-23 07:25:35

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

所以你不能这样做......

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

但这可行:

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

我猜它与内部 .NET Javascript 冲突?

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.

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?

蛮可爱 2024-07-23 07:25:35

另请检查您是否未使用验证组,因为如果设置了验证组属性且未通过显式调用,则验证不会触发

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

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});
跨年 2024-07-23 07:25:35

您是否验证导致回发的控件已将 CausesValidation 设置为 tru 并且没有为其分配验证组?

我不确定还有什么可能导致这种行为。

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?

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

黯然#的苍凉 2024-07-23 07:25:35

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

难道你还有其他未通过的验证吗?

客户端验证未执行,因为您指定了 ClientValidationFunction="TextBoxDTownCityClient" 并且这将查找名为 TextBoxDTownCityClient 的函数作为验证函数,但函数名称应为
TextBoxDAddress1Client

(如您所写)

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

(as you wrote)

暮倦 2024-07-23 07:25:35

感谢您提供有关 ControlToValidate LukeH 的信息!

我在代码中试图做的只是确保当文本字段 B 具有特定值时,某些文本字段 A 在字段中包含一些文本。 否则,A 可以为空或其他任何内容。 摆脱标记中的 ControlToValidate="A" 解决了我的问题。

干杯!

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.

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文