I am trying to call a JavaScript function on an ASP.NET button client click event and want to prevent postback. The function is working, but it is not preventing the page from posting back. Here is my JavaScript code:
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").focus();
e.preventDefault();
return false;
}
if(user2id.value!='' && user2password.value=='')
{
alert("Password is required");
user2id = document.getElementById("txtP2UserPassword").focus();
e.preventDefault();
return false;
}
}
The way I am calling this function is:
<asp:Button runat="server" ID="btnSubmit" OnClientClick="return User2Check();" TabIndex="12" Text="Submit" onclick="btnSubmit_Click" />
Please provide guidance.