Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Experts,

I am Doing an asp.net project. And i need to validate the controls using jquery...

Here is my HTML Code...

ASP.NET
 <form runat="server" class="input-wrapper blue-gradient glossy">
     
	<input type="text" name="login" id="login" value="" class="input-unstyled" placeholder="Login" autocomplete="off"></li>
	<input type="password" name="pass" id="pass" value="" class="input-unstyled" placeholder="Password" autocomplete="off"></li>
	
	<button type="submit" runat="server" onclientclick="javascript:MyFunction();" onserverclick="btn_Login_Click"  class="button glossy float-right" id="btn_Login">Login</button>
		
	
</form>


Here is my Javascript...


JavaScript
function MyFunction() {

           // Values
           var login = $.trim($('#login').val()),
               pass = $.trim($('#pass').val());

           // Check inputs
           if (login.length === 0) {
               // Display message
               alert('Please fill in your login');
               return false;
           }
           else if (pass.length === 0) {
              // Display message
               alert('Please fill in your password');
               return false;
           }
           else {

               return true;

           }

       }



Here is my C# Code


C#
protected void btn_Login_Click(object sender, EventArgs e)
     {
         // code
     }


Actually I need to validate the control First.. If the validation sucess then only goes to code behind ....

When a user Textbox leaves blank then the validation works and don't start the postback...


PLease Help me to do this...

Thanks and Regards,

Dileep..
Posted
Updated 1-Jan-14 20:53pm
v2
Comments
JoCodes 2-Jan-14 2:08am    
Can you check with adding return, onClientClick="return javascript:MyFunction();"
dilzz 2-Jan-14 2:29am    
Now the validation works.. and post back is not happen while returns False.... But in the case of returns "True" didn't work the button click event....
JoCodes 2-Jan-14 3:02am    
Thats what your question was "Actually I need to validate the control First.. If the validation sucess then only goes to code behind"
JoCodes 2-Jan-14 3:35am    
Can you post the changed code?
dilzz 2-Jan-14 3:00am    
When i used "onClientClick" then the client side function is not working only server side is working.....
When i used the "Onclick" then the Client side is working fine... but didn't fire the Server side button click event....
PLease Help me to do this....

Try

<asp:Button runat="server" OnClientClick="if (!MyFunction()) { return false;};" OnClick="btn_Login_Click">


And can also add
UseSubmitBehavior="false"
in case still not firing .
 
Share this answer
 
try like this..



ASP.NET
<asp:Button runat="server" OnClientClick="javascript:return MyFunction();" OnClick="btn_Login_Click"
       class="button glossy float-right" Text="Login" ID="btn_Login">
   </asp:Button >



javascript:return MyFunction();

you should use return keyword...
 
Share this answer
 
v2
Comments
dilzz 2-Jan-14 2:25am    
Now the validation works.. and post back is not happen while returns False.... But in the case of returns "True" didn't work the button click event....
dilzz 2-Jan-14 3:01am    
When i used "onClientClick" then the client side function is not working only server side is working.....
When i used the "Onclick" then the Client side is working fine... but didn't fire the Server side button click event....
PLease Help me to do this....
Karthik_Mahalingam 2-Jan-14 3:27am    
try my updated solution..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900