Reputation: 5249
I am trying to check first if users don not enter either User ID or Password, if either text box is blank or empty then I want to show message box, if they enter both user id and password then I want to execute some other codes. The problem I am having with the code is that the message does not show at all but the good thing is that it does not execute the other code. So I would like to see the message box to show up if both text boxes are empty. I am not sure why the message box is not showing up even though I have used this same code in other functions and it worked. thanks here is my button code for the ASPX
<asp:Panel ID="Panel1" runat="server" BorderStyle="Groove" Height="109px" Visible="false"
Width="870px" BackColor="#FFFFE1">
<asp:Label ID="Label2" runat="server" Text="DIGITAL SIGNATURE" Font-Bold="True"
ForeColor="#FF3300"></asp:Label>
<br />
<br />
<asp:Label ID="lblUID" runat="server" Text="User ID:"></asp:Label>
<asp:TextBox ID="txtUID" runat="server" Height="22px" Width="145px"></asp:TextBox>
<asp:Label ID="lblPass" runat="server" Text="Password:"></asp:Label>
<asp:TextBox ID="txtPass" runat="server" Height="23px" TextMode="Password" style="margin-top: 0px"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="183px"
onclick="btnSubmit_Click"
OnClientClick="return confirm('Are you sure you want to submit?');"
Font-Bold="False" Font-Size="Medium" Height="30px"
style="margin-right: 1px" />
<br />
</asp:Panel>
code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtUID.Text.Length == 0 || txtPass.Text.Length == 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please type your User ID and Password correctly and click Submit button again. Thanks", true);
}
else
{
//execute some code here
}
}
Upvotes: 1
Views: 13427
Reputation: 119
I have created common .js file which contains all client side validation as
function checkMandatory(ele)
{
var text;
text = ele.value;
if(text.length == 0)
{
alert("Please enter value ...");
ele.focus();
return;
}
}
Call function in Page_Load event as txtName.Attributes.Add("onFocusout", "checkMandatory(this);");
Note: Dont forget to include .js file to your page inside HTML tags.
Upvotes: 0
Reputation: 67898
One approach would be to do this client-side. You could change your OnClientClick
to:
return login();
and then build the login
function:
function login() {
if ($('#txtUID').val() === '' ||
$('#txtPass').val() === '') {
alert('Please type your User ID and Password correctly and click Submit button again. Thanks');
return false;
}
return confirm('Are you sure you want to submit?');
}
and so now you can get rid of the server-side code.
Upvotes: 0
Reputation: 94
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtUID.Text.Length == 0 || txtPass.Text.Length == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Please type your User ID and Password correctly and click Submit button again. Thanks');", true);
}
else
{
//execute some code here
}
}
Upvotes: 0
Reputation: 7478
In your code in btnSubmit_Click method, when you call RegisterClientScriptBlock you missed ');
symbols at then end of your alert. So, your javascript is incorrect, and browser displays an error in developers tools. It should be like this:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtUID.Text.Length == 0 || txtPass.Text.Length == 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"alertMessage",
"alert('Please type your User ID and Password correctly and click Submit button again. Thanks');", true);
}
else
{
//execute some code here
}
}
Upvotes: 4