Search code examples
asp.netjquery-uivalidationautocompletecustomvalidator

ASP CustomValidator doesn't detect text changed with JQuery autocomplete


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.


Solution

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