ASP CustomValidator doesn't detect text changed with JQuery autocomplete

淺唱寂寞╮ 提交于 2019-12-11 10:46:40

问题


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.


回答1:


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');
});



回答2:


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.




回答3:


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

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



回答4:


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



来源:https://stackoverflow.com/questions/7633505/asp-customvalidator-doesnt-detect-text-changed-with-jquery-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!