Call javascript function on asp.net validator fail

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

thanks! Mike
cmushaunAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
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();" />
with
<asp:Button id="btnSubmit" Text="Submit" onClientClick="btnClick" onclick="btnSubmit_Click" />
0
 
remorinaCommented:
Hi cmushaun,

Try this http://forums.asp.net/p/1398068/3098878.aspx#3098878 and let me know if it helps

Cheers
0
 
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 ) {
alert('hi');

}

}
</script>


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

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

 
Gagan_JauraCommented:
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');
             else
                alert('Validation Fails');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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
         </asp:RequiredFieldValidator>
        <input id="btnSubmit" type="button" value="Submit"
           onclick="btnClick();" /></div>
    </form>
</body>
</html>

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.

0
 
cmushaunAuthor Commented:
Gagan_Jaura, Doesn't this way of calling the javascript remove calling of the code_behind method I'm calling:"Button1_Click"?
0
 
Gagan_JauraCommented:
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');
0
 
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?
0
All Courses

From novice to tech pro — start learning today.