Derin
Derin

Reputation: 2185

Calling a Javascript function when Required field validator is true

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.

Upvotes: 2

Views: 15278

Answers (3)

M4N
M4N

Reputation: 96626

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> 

Upvotes: 2

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11309

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();" />

Upvotes: 8

Akram Shahda
Akram Shahda

Reputation: 14781

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

Upvotes: 1

Related Questions