Call javascript function on validator fail

I have a required field validator in an c# web application.  Everytime the validator fails, I need to call a JS function.

thanks! Mike
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Gagan_JauraConnect With a Mentor Commented:
I created a sample and added few validations at server side. And they are working fine. The first solution given by me is working good for me. I have made only one change in my code. I have changed the below code
<input id="btnSubmit" type="button" value="Submit" onclick="btnClick();" />
<asp:Button id="btnSubmit" Text="Submit" onClientClick="btnClick" onclick="btnSubmit_Click" />
Hi cmushaun,

Try this and let me know if it helps

cmushaunAuthor Commented:
i'm trying to use the code below but I this doesn't call the alert('hi') until the second time I click the button and the page is marked invalid.  Any ideas?

<script language="javascript" type="text/javascript">
function fnShow() {

if ( Page_IsValid == false ) {



<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="javascript:fnShow();" >
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

You can do a smart work here. You can call the javascript function and from that function you initiate validators on you page. Below is the samle code
<head runat="server">
    <script language="javascript" type="text/javascript">
     function btnClick()
        if (Page_ClientValidate() == true)            
                alert('Validation Pass');
                alert('Validation Fails');
    <form id="form1" runat="server">
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
          runat="server" ErrorMessage="Please enter your name"
          ControlToValidate="txtName">Please enter your name
        <input id="btnSubmit" type="button" value="Submit"
           onclick="btnClick();" /></div>

Here if the validator fails, the Page_ClientValidate() function will return false and then you can write your javascript code in the else condition of btnClick() function.

cmushaunAuthor Commented:
Gagan_Jaura, Doesn't this way of calling the javascript remove calling of the code_behind method I'm calling:"Button1_Click"?
It will definately call "Button1_Click" event because the flow of the page is maintained. If you simply use validator (without using javascript) then also internally Page_ClientValidate() function is called when the event is fired and then "Button1_Click".
If at all in the worst case, the system do not fire "Button1_Click" event then you can put __doPostBack('Button1_Click','') after alert('Validation Pass');
cmushaunAuthor Commented:
I tried this, but I don't think this method will work because after the initial required field validation, I have other validation that occurs in the code-behind. I think actually the javascript method needs to be called after the button1_click is called and the page posts back.   Is this possible?
All Courses

From novice to tech pro — start learning today.