如果用户名和密码错误,如何更改文本框的颜色 [英] How to change the colour of textbox in case of wrong username and password

查看:87
本文介绍了如果用户名和密码错误,如何更改文本框的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入正确的密码和用户名时,我想更改文本框的颜色..请告诉我该怎么做



I want to change the colour of textbox when user gives in correct password and username ..please tell me how would i do this

<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" />

推荐答案

您可以使用css(如果有)检查验证
You can use css if any for checking validation
.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;
}



和对于wromg用户名或传递的文本框颜色更改使用此


and for textbox color change in case of wromg user name or pass use this

textBox.TextChanged += (sender, args) => {
    int ignored;
    bool valid = int.tryParse(textBox.Text, out ignored);
    textBox.ForeColor = valid ? Color.Black : Color.Red;
};


创建两个CssClass,一个用于普通文本框,一个用于在无效尝试登录后设计它。



根据您的要求更改代码:
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:
if(!IsPostBack)
   TextBox1.CssClass = "txtnormal";//set normal style on first load

protected void btnLogin_Click(object sender, EventArgs e)
{
    //code to validate the user
    TextBox1.CssClass = "txtinvalid";//change CssClass on invalid attempt
}



问候......


Regards...


请参阅我已将您的用户名输入文本框更改为asp:TextBox

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" />



在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

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;
       }
   }


这篇关于如果用户名和密码错误,如何更改文本框的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆