2

I would like to have a Javascript function to be called when ever my required field validator control is true (i.e when the validator control is fired / error message shown).

Kindly let me know how this can be done.

Thanks in advance.

3 Answers 3

8

You can call a JS function on OnClientlick of the button.

for ex.

function CheckValidation() 
    {
        if (Page_ClientValidate())
            {
               // Call Your custom JS function and return value.
            }
    }

// Calling JS function

<asp:Button ID="btnSubmit" runat="server" 
OnClientClick="return CheckValidation();" />
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you're validating a TextBox control, the following snippet should do what you want:

<asp:TextBox id=txtZip runat=server OnChange="txtZipOnChange();" />
<asp:RegularExpressionValidator id="valZip" runat="server"
   ControlToValidate="txtZip" ...>

<script>
function txtZipOnChange() {
   // get the validator and check if it is valid
   var val = <%= valZip.ClientID %>;
   if (val.isvalid == false) {
     // do something
   }
}
</script> 

Comments

1

Use CustomValidator control. Set the CustomValidator.ClientValidationFunction property to the javascript function and the CustomValidator.ValidateEmptyText property to false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.