user576510
user576510

Reputation: 5905

JavaScript - return false from function is not working?

I m calling a javascript function on asp.net button client click and want to prevent post back. function works but it do not stop to be posted back. My Javascript is:

function User2Check()
    {
        var user2id=document .getElementById("txtP2UserName");
        var user2password=document .getElementById("txtP2Password");

        if(user2id.value=='' & user2password.value!='')
        {
            alert("User name is required");
            user2id=document .getElementById("txtP2UserName").foucs();
            e.preventDefault();                
            return false;
        }
        if(user2id.value!='' & user2password.value=='')
        {
            alert("Password is required");
            user2id=document .getElementById("txtP2UserPassword").foucs();
            e.preventDefault();                
            return false;

        }


    }

The I am calling this function is:

<asp:Button runat="server" ID="btnSubmit" OnClientClick="return User2Check();" TabIndex="12" Text="Submit" onclick="btnSubmit_Click" />

plz guide.

Upvotes: 0

Views: 3776

Answers (5)

Hitesh Prajapati
Hitesh Prajapati

Reputation: 2842

You use below code and remove " e.preventDefault();" from code.

function User2Check()
{
var user2id=document .getElementById("txtP2UserName");
var user2password=document .getElementById("txtP2Password");

if(user2id.value=='')
{
    alert("User name is required");
    user2id=document .getElementById("txtP2UserName").focus();
    return false;
}
if(user2password.value=='')
{
    alert("Password is required");
    user2id=document .getElementById("txtP2UserPassword").focus();
    return false;
}
  return true;
}

and in server side code use

<asp:Button runat="server" ID="btnSubmit" OnClientClick="return User2Check();" TabIndex="12" Text="Submit" onclick="btnSubmit_Click" />

Upvotes: 1

Gaurav
Gaurav

Reputation: 28755

you should use && instead of &

if(user2id.value!='' && user2password.value=='')

AND : Function needs an argument, but you are calling with no argument

You can do it as

function User2Check(e)
{
    var user2id=document .getElementById("txtP2UserName");
    var user2password=document .getElementById("txtP2Password");

    if(user2id.value=='')
    {
        alert("User name is required");
        user2id=document .getElementById("txtP2UserName").focus();
        e.preventDefault();                
        return false;
    }
    if(user2password.value=='')
    {
        alert("Password is required");
        user2id=document .getElementById("txtP2UserPassword").focus();
        e.preventDefault();                
        return false;
    }

return true;

}

<asp:Button runat="server" ID="btnSubmit" OnClientClick="return User2Check(event);" TabIndex="12" Text="Submit" onclick="btnSubmit_Click" />

Upvotes: 1

Shaz
Shaz

Reputation: 15867

Since you don't appear to be returning true, put an exclamation mark before the function. For example:

!function(){
    // Code here!
}

Upvotes: -1

Steve Wellens
Steve Wellens

Reputation: 20620

You've misspelled foucs();

The code will stop running after that error.

Upvotes: 0

Bala R
Bala R

Reputation: 108947

Your javascript takes a parameter but the call from the button's OnClientClick has no parameter. I would think since e is null, the function terminates or returns before returning false but since focus() calls are before calling anything on e, the function seems to be working.

Upvotes: 1

Related Questions