Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to run my java script code for validate if i click the button.but its not working

<asp:Button ID="Button1" runat="server" Text="save" OnClick="Button1_Click" onOnClientClick="uservalid();"/>
Posted

Edit:
The OP commented: This was just a typo in the question, not actually in the code
onOnClientClick should be OnClientClick

Edit:

Attempt 2.

The client click will not do much because the page is being posted anyway. Instead of OnClick="Button1_Click" try this (or similar)
ASP.NET
<asp:Button ID="Button1"
   runat="server" Text="save"
   onOnClientClick="uservalid(this);"/>

<script ...>
  function uservalid(control){
     controle.preventDefault(); //stops the postback.
     bool isValid = someotherstuff();
     if(isValid)
       __doPostBack(control.id,null);
  }
</script>


This is not tested but I've done similar before. The postback function should cause the click event to be triggered.

You will still have to wire up the click event in the code behind but have autopostback = false so the for is only posted if the user validation passes
 
Share this answer
 
v2
Comments
Member 11507028 27-May-15 12:28pm    
sorry for that the actual code was
<asp:Button ID="Button1" runat="server" Text="save" OnClick="Button1_Click" OnClientClick="uservalid();" />
Andy Lanng 27-May-15 12:29pm    
Ah ok - I'll update my solution
Member 11507028 27-May-15 14:08pm    
no result
Andy Lanng 28-May-15 3:52am    
Please update the question with the code you have (including code behind) so I can test it locally.

You don't need to post the whole thing. Just the markup and the events where you set the OnClick
Member 11507028 28-May-15 10:19am    
when i am calling the jaavascript method to be used when button clicked its not working no validaation done..

<asp:Button ID="Button1" runat="server" Text="save" OnClick="Button1_Click" OnClientClick="uservalid();" />
You could do it all solely via jquery?


var validateFunction = function(){
//your validate function goes here.
}

// put this in your functions initalizer.
$('#idOfButton').on('click', validateFunction);
 
Share this answer
 
You need to add "return" and your "uservalid" js function needs to "return false;" if validation fails and "return true;" if it doesn't.

<asp:Button ID="Button1" runat="server" Text="save" OnClick="Button1_Click" onOnClientClick="return uservalid();"/> 


Remember that I can disable your client validation so make sure you validate on the server too.
 
Share this answer
 
Comments
Member 11507028 29-May-15 0:34am    
same proble still not working ,actually its not finding the the path of the java script written
F-ES Sitecore 29-May-15 3:40am    
You're going to have to give more information than things like "not working", we can't access your system to perform diagnostics. Do you get console errors? Is the js function on the same page or in an include? If an include is it loaded ok? Does the function get called at all?
Member 11507028 29-May-15 7:33am    
thats its not calling the function on button click
function uservalid()
{

var name, userid, password, copmarepassword, mailid, dateofbirth, gender, emailexp;

name = document.getElementById("TextBox1").value;
userid = document.getElementById("TextBox2").value;
password = document.getElementById("TextBox3").value;
copmarepassword = document.getElementById("TextBox4").value;
mailid = document.getElementById("TextBox5").value;
dateofbirth = document.getElementById("TextBox6").value;
gender = document.getElementById("Ddl").value;
emailexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/;

if (name == '' && userid == '' && password == '' && copmarepassword == '' && mailid == '' && dateofbirth == '' && gende == '')

{
alert("enter all fields");
return false;
}
if (name == '') {
alert("enter username");
return false;
}
if (userid =='') {
alert("enter userid");
return false;
}
if (gender == 0) {
alert("Please Select gender");
return false;
}
if (password == '') {
alert("Please Enter Password");
return false;

}
if (password != '' && copmarepassword == '');
{
alert("please conform password");
return false;

}
if (password != copmarepassword) {
alert("password do not match");
return false;
}
if (mailid == '') {
alert("enter mailid");
return false;


}
if (mailid != '') {
if (mailid.match(emailexp))
{
alert("invalid mailid");
return false
}

}

if (dateofbirth == '')
{
alert("enter date of birth");
}
return true;
}
 
Share this answer
 

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