Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts

I had a javascript function. I am calling the function in a button click.
Its working fine. But the issue is i am getting the return value as false from the script. After getting false my button click event is getting firing. How to avoid this. Why it is happening.

Rgds
Jagadesh
Posted
Comments
Prasad_Kulkarni 14-Jun-13 4:13am    
Can you post your code snippets?
Kumar from madras 14-Jun-13 4:18am    
Hi Prasad

Hope you understood my Question. I Pasted my Javascript.

<script src = 'Scripts/jquery-1.10.1.min.js' type ="text/javascript"></script>

<script type = "text/javascript">
function validatebutton1(txtusrname, txtpassword)

{
var txtusr = document.getElementById(txtusrname).value;
var txtpassw = document.getElementById(txtpassword).value;

if (txtusr == "") {
if (txtpassw == "") {

alert("Please Enter the userid and password");
document.getElementById(txtusrname).focus();
return false;
}

}
if (txtusr == "") {
if (txtpassw != null) {

alert("Please enter the USERID");
document.getElementById(txtusrname).focus();
return false;
}}
if (txtpassw == "") {
if (txtusr != null) {

alert("Please enter the Password");
document.getElementById(txtpassword).focus();
return false;
}
}

}


</script>
Can you show the button aspx code?
Member 9773317 14-Jun-13 5:14am    
what values are you passing in function validatebutton1(txtusrname, txtpassword)
If u are passing the values of username and password then there is no need for again fetching the values as :
var txtusr = document.getElementById(txtusrname).value;
var txtpassw = document.getElementById(txtpassword).value;
Kumar from madras 14-Jun-13 5:19am    
I am passing my Control ids for this function.

Write return statement while calling javascript. So, whenever javascript will return false Button1 will not allow to submit form.
Try this:
HTML:
ASP.NET
<asp:Button ID="Button1" runat="server" Text="My Button" OnClientClick="return MyFunction();" />

Javascript:
JavaScript
function MyFunction()
{
    return false;
}


[Edit 1]
Since you are passing the parameters in function, you can add the attributes to button from code behind from Page_Load event. Try this in your Page_Load event:
C#:
C#
Button1.Attributes.Add("onclick", "return validatebutton1('" + txtUserName.ClientID + "', '" + txtPassword.ClientID + "');")

Let you javascript be the same.
HTML:
ASP.NET
<asp:Button ID="Button1" runat="server" Text="My Button" />


--Amit
 
Share this answer
 
v3
Comments
Kumar from madras 14-Jun-13 4:36am    
HI AMIT

I already tried using Return all. I did'n get the solution.

Thnks for your response
_Amy 14-Jun-13 4:42am    
Try my updated answer. :)
Kumar from madras 14-Jun-13 4:55am    
I also tried this. Pls view my code below.

btnLogin.Attributes.Add("onclick", "javascript:return validatebutton1('TxtUsrname','Txtpassword')");
Try this:
JavaScript
<script type="text/javascript"> 
function validatebutton1() 
{ 
var txtusr = document.getElementById('yourUserTextBoxClientID').value; //replace
var txtpassw = document.getElementById('yourPasswordTextBoxClientID').value; //replace

if(txtusr == '' && txtpassw == '')
{
alert('Please enter user name and password.');
return false;
}

if(txtusr == '')
{
alert('Please enter user name.');
return false;
}

if(txtpassw == '')
{
alert('Please enter password.');
return false;
}
return true;
}
</script>
 
Share this answer
 
v3
Comments
Kumar from madras 14-Jun-13 5:06am    
Hi Zafar

Thanks for your comment

Even i tried with only 1 if condition. I had the same problem.

Rgds
Jagadesh
Zafar Sultan 14-Jun-13 5:20am    
Do one thing. Add in your page load event:
btnLogin.Attributes.Add("onclick", "return validatebutton1();");

Run your page and view its source. Get the id's(from page source) of your textboxes and replace them in my updated solution. See if it works.
As per as i understand your code,you are trying to check weather someone is trying to click button without providing username and password? If this is so,why dont you use requiredfield validator like this one.

C#
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown" />

<asp:requiredfieldvalidator id="RequiredFieldValidator2" controltovalidate="TextBox1" display="Static" errormessage="*" initialvalue="" runat="server" validationgroup="A" xmlns:asp="#unknown" /> 

<asp:button id="btn_submit" runat="server" text="Submit" onclick="btn_submit_Click" validationgroup="A" xmlns:asp="#unknown" />


If you implement this code,it shows errormessage(*) that says this field is mendatory.

Regards.. :laugh:
 
Share this answer
 
Comments
Kumar from madras 14-Jun-13 5:18am    
HI Rohan

I done this on Required Field Validator. I am looking forward to achieve this using Javascript or JQUERY
Thanks7872 14-Jun-13 5:20am    
use improve question widget on your question and post the latest code you have now.comment me after that.
Hi,

You must do return true in one condition in which your condition are true (i.e. your uname and pwd are filled.)

Hope by this way it will work.
 
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