Search code examples
jqueryasp.netcustomvalidator

Invoking CustomValidator validation from JavaScript


I have the following setup, where I am custom validating a textbox. It works fine as long as I am manually typing in the textbox and changing the text and focusing out of the textbox.

<asp:TextBox ID="tbpCity" runat="server"/>     
<asp:CustomValidator ID="cvPermanentCity" runat="server" ControlToValidate="tbpCity" 
 ErrorMessage="CustomValidator" onservervalidate="Field_ServerValidate"   SetFocusOnError="true" Display="Dynamic" ToolTip="PermanentCity" />
 <ajaxtoolkitwcsfextensions:ServerSideValidationExtender ID="PermanentCityServerSideValidationExtender" runat="server" TargetControlID="cvPermanentCity" />

when I try to invoke validation change event from javascript (using JQuery 1.4.2)

function copyCity() {
$('#<%= tbpCity.ClientID%>').value = "Some City";
$('#<%= tbpCity.ClientID%>').trigger("change");
}

the custom validation is not being invoked.

How can I inovke customvalidator to do validation?

note: I have verified this works on FireFox but not on IE. Please let me know how to fire change event in IE.


Solution

  • I have found the answer posted to similar question on StackOverflow.

    var tbPermanentAddressCity = document.getElementById('<%= tbpCity.ClientID%>');
    if (tbPermanentAddressCity.fireEvent) {
        tbPermanentAddressCity.fireEvent("onchange");
    } else {
        $('#<%= tbpCity.ClientID%>').change();
    }
    

    Once the onchange event is fired, the CustomValidator picks up and validates the textbox.