ASP CustomValidator 无法检测通过 JQuery 自动完成更改的文本

发布于 2024-12-07 14:55:09 字数 695 浏览 0 评论 0原文

我编写了一个网页,它使用 ASP.NET CustomValidator 来验证文本字段(客户端)的输入。文本字段使用 JQuery UI 自动完成 - 这就是我遇到问题的地方。

验证器工作得很好。但是,如果验证失败并且用户收到错误消息,用户将返回文本字段并输入新值 - 从自动完成插件创建的下拉列表中进行选择。现在,当选择新值并且用户离开输入字段时,验证代码不会再次触发。我怀疑这是因为,由于某种原因,当值来自自动完成帮助程序时,没有检测到文本已更改。这有道理吗?

有谁知道当用户从字段中删除焦点时如何强制字段通过 CustomValidator 进行验证?

这是 CustomValidator:

<asp:CustomValidator EnableClientScript="True" runat="server" ControlToValidate="tbInput"
                    ID="inputCustomValidator" ClientValidationFunction="validateFunction" ErrorMessage="Not valid"
                    Display="Dynamic" ValidationGroup="ValidationGrp1" />

被调用的 javascript 并不有趣 - 因为它没有被调用。这就是我想要实现的目标。

I've written a web page that uses an ASP.NET CustomValidator to validate the input of a text field (client side). The text field uses JQuery UI Autocomplete - and this is where I run into problems.

The validator works just fine. But in the event that the validation fails and the user gets an error message, the user will go back to the text field and enter a new value - choosing from the drop down made by the autocomplete plugin. Now when a new value has been chosen and the user leaves the input field, the validation code doesn't fire again. I suspect that it is because, for some reason, it is not detected that the text was changed, when the value came from the autocomplete helper. Does that make sense?

Does anyone know how I can force the field to validate via the CustomValidator when the user removes focus from the field?

Heres the CustomValidator:

<asp:CustomValidator EnableClientScript="True" runat="server" ControlToValidate="tbInput"
                    ID="inputCustomValidator" ClientValidationFunction="validateFunction" ErrorMessage="Not valid"
                    Display="Dynamic" ValidationGroup="ValidationGrp1" />

The javascript that is called is not interesting - since it is not being called. That is what I want to achieve.

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

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

发布评论

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

评论(4

流年已逝 2024-12-14 14:55:09

我找到了一种方法来做到这一点。在 javascript 中,我向我想要验证的元素添加了一个 Blur() 函数,并使该函数触发 Page_ClientValidate('validationGroupName')。对我来说这是一个新功能。

$('.elementToValidate').blur(function () {
Page_ClientValidate('ValidationGrp1');
});

I found a way to do this. In javascript I added a blur() function to the element I wanted to validate and made that function trigger Page_ClientValidate('validationGroupName'). A functionality that was new to me.

$('.elementToValidate').blur(function () {
Page_ClientValidate('ValidationGrp1');
});
抱猫软卧 2024-12-14 14:55:09

这可能不是您想听到的,但我会尽可能避免混合 jQuery 和 ASP.net 的 Javascript - 它们往往不能很好地发挥作用。

对于您的情况,我建议转储 ASP.net CustomValidator 并切换到 jQuery 的 验证插件。这将与 jQuery UI 配合得更好。

This might not be what you want to hear, but I would avoid mixing jQuery and ASP.net's Javascript whenever possible - they don't tend to play nicely.

In your case, I'd recommend dumping your ASP.net CustomValidator and switching to jQuery's Validate plugin. That will play much more nicely with jQuery UI.

情魔剑神 2024-12-14 14:55:09

您还可以使用 autoselect 的 select: 处理程序来触发验证。

$(".someClass").autocomplete({
select: function(event, ui)
{
Page_ClientValidate(ui);
}
});

You can also use autoselect's select: handler to trigger validation.

$(".someClass").autocomplete({
select: function(event, ui)
{
Page_ClientValidate(ui);
}
});
堇年纸鸢 2024-12-14 14:55:09

我通过为文本框设置 onBlur 事件来实现此功能。

<asp:TextBox ID="TextBox1" runat="server" onBlur="Page_ClientValidate('ValidationGrp1');"></asp:TextBox>

(假设您的 CustomValidator 的 ValidationGroup 是“ValidationGrp1”)

I got this working by setting the onBlur event for my textbox.

<asp:TextBox ID="TextBox1" runat="server" onBlur="Page_ClientValidate('ValidationGrp1');"></asp:TextBox>

(assuming your CustomValidator's ValidationGroup is 'ValidationGrp1')

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