I have these codes to validate the username and password entry. If the username is correct and the password is wrong, the html JavaScript validates and shows alert to notify me.
However, I also want to show a message in the code behind right after the html JavaScript alert. According to the code below, only the html alert shows.
Something blocks the code behind message and doesn't show.
Would you review the issue and let me know the solution please?
<script type="text/javascript">
function ValidateRequiredFields() {
$(document).on('click', '[ID*=ButtonLogin]', function () {
if (document.getElementById("<%=txtUsername.ClientID%>").value === '') {
alert("Username field is empty, Please fill in a Username.");
document.getElementById("<%=txtUsername.ClientID%>").focus();
return false();
}
else if (document.getElementById("<%=txtPassword.ClientID%>").value === '') {
alert("Password field is empty, Please fill in a Password.");
document.getElementById("<%=txtPassword.ClientID%>").focus();
return false();
}
else if (document.getElementById("Username_Check").innerHTML === 'Invalid Username and or Password!') {
alert("Invalid Username and or Password!");
document.getElementById("<%=txtUsername.ClientID%>").focus();
return false();
}
else if (document.getElementById("Password_Check").innerHTML === 'Invalid Username and or Password!') {
alert("Invalid Username and or Password!");
document.getElementById("<%=txtPassword.ClientID%>").focus();
return false();
}
});
}
</script>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonLogin">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<span id="Username_Check"></span>
<span id="Password_Check"></span>
<asp:TextBox ID="txtUsername" runat="server" autocomplete="off" onChange="return UsernameAvailability();" onkeyup="return UsernameAvailability();" ondrop="return false;" onpaste="return false;">
</asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server" autocomplete="off" onChange="return PasswordAvailability();" onkeyup="return PasswordAvailability();" ondrop="return false;" onpaste="return false;" TextMode="Password"></asp:TextBox>
<asp:Button ID="ButtonLogin" runat="server" Text="Log In" BackColor="Red" Font-Bold="true" OnClientClick="return ValidateRequiredFields();" OnClick="ButtonLogin_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonLogin" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</asp:Content>
//Retrieve the Salt from the database and use it to hash the input password.
string consString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(consString))
{
using (SqlCommand getUserCmd = new SqlCommand("SELECT Salt, PasswordHash FROM Profile WHERE Username LIKE @Name", conn))
{
getUserCmd.Parameters.AddWithValue("@Name", txtUsername.Text + "%");
conn.Open();
using (SqlDataReader reader = getUserCmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
//This JavaScript message doesn’t open.
string RemainTime = 2.ToString();
string ms = "Please try again after " + RemainTime + " minutes.";
string sc = "alert('" + ms.Replace("\n", "\\n") + "');";
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "alertScript", sc, true);
}
}
}
}
}