Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change the colour of textbox when user gives in correct password and username ..please tell me how would i do this

ASP.NET
<form id="form1" runat="server">
           <h2>
               Login
           </h2>
           <p>
               <input id="userName" type="text" placeholder="Username" runat="server" /></p>
           <p id="readonlyUserName" class="hide">
               <img src="images/administrator.png" class="center" width="55px" height="55px" alt="account picture" /></p>
           <p>
               <input id="password" type="password" placeholder="Password" runat="server" /></p>
           <label for="remember">
               <input type="checkbox" id="remember" value="remember" runat="server" />
               <span>Remember me on this computer</span>
           </label>
           <asp:Button ID="LoginButton" CssClass="button loginButton" runat="server" Enabled="true"
               OnClick="LoginButton_Click" />
Posted
Updated 7-Nov-13 19:49pm
v2
Comments
CodeBlack 8-Nov-13 1:31am    
can you show me the code for LoginButton_Click
Thomas ktg 8-Nov-13 1:35am    
use this code to change the textbox color whe the username and password are incorrect.
TextBox1.BackColor = System.Drawing.Color.LavenderBlush;
CodeBlack 8-Nov-13 1:47am    
instead of replying. create a new comment so that it can notify the person who asked the question. use reply button when required.
lukeer 8-Nov-13 1:37am    
1) Don't repost for providing further information. Use the "Improve question" link instead.
2) Enclose code in "pre" tags. They look like this: <pre lang="XML">YourCodeHere();</pre>

I did this in your original question and will vote this as repost.

You can use css if any for checking validation
CSS
.newtext
{
    border: 1px solid #55aad1;
    background: #fff;
    color: #333;
    font-size: 1.2em;
    margin: 5px 0 6px 0;
    padding: 5px;
    width: 300px;
}
.newtext:focus
{
    border: 1px solid #55aad1;
}
.newtext-validation-error
{
    border: 1px solid #e80c4d;
    background-color: #fc7e7e;
}

and for textbox color change in case of wromg user name or pass use this
C#
textBox.TextChanged += (sender, args) => {
    int ignored;
    bool valid = int.tryParse(textBox.Text, out ignored);
    textBox.ForeColor = valid ? Color.Black : Color.Red;
};
 
Share this answer
 
Create two CssClass,one for normal textbox and one for designing it after invalid attempt for login.

Change it as per your requirement in code:
C#
if(!IsPostBack)
   TextBox1.CssClass = "txtnormal";//set normal style on first load
C#
protected void btnLogin_Click(object sender, EventArgs e)
{
    //code to validate the user
    TextBox1.CssClass = "txtinvalid";//change CssClass on invalid attempt
}

Regards...
 
Share this answer
 
v2
See I have changed your user name input textbox to asp:TextBox
<asp:TextBox ID="userName" type="text" placeholder="Username" runat="server" AutoPostBack="True" OnTextChanged="userName_TextChanged" />

Added on Textchanged event where you will check this user name to your database username if this is not exist then throw error message with textbox color change

C#
protected void userName_TextChanged(object sender, EventArgs e)
   {
       //In this case I have just taken a demo name, But you will check your textbox value to database
       //If it will not exist then throw the error message
       if (userName.Text != "sankar")
       {
           userName.BackColor = System.Drawing.Color.Red;
           Response.Write("Wrong Username");
       }
       else {
           userName.BackColor = System.Drawing.Color.White;
       }
   }
 
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