C# 如何使用TextChanged从字段B重新检查字段A?
我这里有点小问题。以下是我的情况: 我键入用户名:tester(有效用户名;避免所有检查),然后键入密码:testerr(有效密码;避免所有检查)。问题是我正在检查相同的输入。在我的代码中,当两个输入相同时,我将看到一个通知。现在,当我输入tester作为用户名和密码时,我得到了错误,但是当我在我的密码'testerr'中添加额外字符时,我使密码有效,但是用户名被检查为无效,说明两者仍然相同,使我的验证无法进行 如何避免这种情况?我想从字段2中重新检查用户名字段,但我不确定如何检查C# 如何使用TextChanged从字段B重新检查字段A?,c#,.net,validation,textchanged,C#,.net,Validation,Textchanged,我这里有点小问题。以下是我的情况: 我键入用户名:tester(有效用户名;避免所有检查),然后键入密码:testerr(有效密码;避免所有检查)。问题是我正在检查相同的输入。在我的代码中,当两个输入相同时,我将看到一个通知。现在,当我输入tester作为用户名和密码时,我得到了错误,但是当我在我的密码'testerr'中添加额外字符时,我使密码有效,但是用户名被检查为无效,说明两者仍然相同,使我的验证无法进行 如何避免这种情况?我想从字段2中重新检查用户名字段,但我不确定如何检查 bool p
bool passIsValid, userIsValid;
public AuthenticationWindow()
{
InitializeComponent();
// Creating TextChanged events which till validate input fields.
txtUserName.TextChanged += new EventHandler(txtUserName_TextChanged);
txtPassword.TextChanged += new EventHandler(txtPassword_TextChanged);
}
private void txtUserName_TextChanged(object sender, EventArgs e)
{
// Checking for empty user name field.
if (string.IsNullOrEmpty(txtUserName.Text))
{
lblMessageUser.Text = "User Name field cannot be empty!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Making sure that user name is at least 6 characters long.
else if (txtUserName.Text.Length < 6)
{
lblMessageUser.Text = "User Name field must be at least 6 characters long!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Checking for user name made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtUserName.Text.Distinct().Skip(1).Any())
{
lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Making sure that password and user name aren't the same.
else if (txtUserName.Text == txtPassword.Text)
{
lblMessageUser.Text = "User Name and Password can not be the same!";
lblMessagePass.Text = "User Name and Password can not be the same!";
lblMessageUser.ForeColor = Color.IndianRed;
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
passIsValid = false;
}
// If all other checks aren't trigered; enable authentication.
else
{
lblMessageUser.Text = "User Name is valid.";
lblMessageUser.ForeColor = Color.Green;
userIsValid = true;
if (passIsValid && userIsValid)
{
btnAuthenticate.Enabled = true;
}
}
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
// Checking for Null or Empty string in password field.
if (string.IsNullOrEmpty(txtPassword.Text))
{
lblMessagePass.Text = "Password field cannot be empty!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Making sure that password is at least 6 characters long.
else if (txtPassword.Text.Length < 6)
{
lblMessagePass.Text = "Password field must be at least 6 characters long!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Checking for password made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtPassword.Text.Distinct().Skip(1).Any())
{
lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Making sure that user name and password are not the same.
// Security measure.
else if (txtUserName.Text == txtPassword.Text)
{
lblMessageUser.Text = "User Name and Password can not be the same!";
lblMessagePass.Text = "User Name and Password can not be the same!";
lblMessageUser.ForeColor = Color.IndianRed;
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
passIsValid = false;
}
// If all other checks aren't trigered; enable authentication.
else
{
lblMessagePass.Text = "Password is valid.";
lblMessagePass.ForeColor = Color.Green;
passIsValid = true;
if (passIsValid && userIsValid)
{
btnAuthenticate.Enabled = true;
}
}
}
boolpassisvalid,userIsValid;
公共身份验证窗口()
{
初始化组件();
//创建文本更改事件,直到验证输入字段。
txtUserName.TextChanged+=新事件处理程序(txtUserName\u TextChanged);
txtPassword.TextChanged+=新事件处理程序(txtPassword\u TextChanged);
}
私有void txtUserName_TextChanged(对象发送方,事件参数e)
{
//正在检查空用户名字段。
if(string.IsNullOrEmpty(txtUserName.Text))
{
lblMessageUser.Text=“用户名字段不能为空!”;
lblMessageUser.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
}
//确保用户名长度至少为6个字符。
else if(txtUserName.Text.Length<6)
{
lblMessageUser.Text=“用户名字段长度必须至少为6个字符!”;
lblMessageUser.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
}
//正在检查由相同重复字符组成的用户名。
//无效示例:“AAAAA”
如果(!txtUserName.Text.Distinct().Skip(1.Any()),则为else
{
lblMessageUser.Text=“用户名不能重复相同的字符!”;
lblMessageUser.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
}
//确保密码和用户名不相同。
else if(txtextername.Text==txtPassword.Text)
{
lblMessageUser.Text=“用户名和密码不能相同!”;
lblMessagePass.Text=“用户名和密码不能相同!”;
lblMessageUser.ForeColor=Color.IndianRed;
lblMessagePass.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
passIsValid=false;
}
//如果未触发所有其他检查,则启用身份验证。
其他的
{
lblMessageUser.Text=“用户名有效。”;
lblMessageUser.ForeColor=Color.Green;
userIsValid=true;
if(passIsValid&&userIsValid)
{
btnAuthenticate.Enabled=true;
}
}
}
私有void txtPassword_TextChanged(对象发送方,事件参数e)
{
//在密码字段中检查空字符串或空字符串。
if(string.IsNullOrEmpty(txtPassword.Text))
{
lblMessagePass.Text=“密码字段不能为空!”;
lblMessagePass.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
passIsValid=false;
}
//确保密码长度至少为6个字符。
else if(txtPassword.Text.Length<6)
{
lblMessagePass.Text=“密码字段长度必须至少为6个字符!”;
lblMessagePass.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
passIsValid=false;
}
//正在检查由相同重复字符组成的密码。
//无效示例:“AAAAA”
如果(!txtPassword.Text.Distinct().Skip(1.Any()),则为else
{
lblMessagePass.Text=“密码不能重复相同的字符!”;
lblMessagePass.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
passIsValid=false;
}
//确保用户名和密码不相同。
//安全措施。
else if(txtextername.Text==txtPassword.Text)
{
lblMessageUser.Text=“用户名和密码不能相同!”;
lblMessagePass.Text=“用户名和密码不能相同!”;
lblMessageUser.ForeColor=Color.IndianRed;
lblMessagePass.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
passIsValid=false;
}
//如果未触发所有其他检查,则启用身份验证。
其他的
{
lblMessagePass.Text=“密码有效。”;
lblMessagePass.ForeColor=Color.Green;
passIsValid=true;
if(passIsValid&&userIsValid)
{
btnAuthenticate.Enabled=true;
}
}
}
您可以将两个事件合并为一个事件,然后用户会看到所有错误,您的检查可能会成功
bool passIsValid, userIsValid;
public AuthenticationWindow()
{
InitializeComponent();
// Creating TextChanged events which till validate input fields.
txtUserName.TextChanged += new EventHandler(txtCheck_TextChanged);
txtPassword.TextChanged += new EventHandler(txtCheck_TextChanged);
}
private void txtCheck_TextChanged(object sender, EventArgs e)
{
// Checking for empty user name field.
if (string.IsNullOrEmpty(txtUserName.Text))
{
lblMessageUser.Text = "User Name field cannot be empty!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Making sure that user name is at least 6 characters long.
else if (txtUserName.Text.Length < 6)
{
lblMessageUser.Text = "User Name field must be at least 6 characters long!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Checking for user name made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtUserName.Text.Distinct().Skip(1).Any())
{
lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
else
{
userIsValid = true;
lblMessageUser.Text = "Password is valid.";
lblMessageUser.ForeColor = Color.Green;
}
// Checking for Null or Empty string in password field.
if (string.IsNullOrEmpty(txtPassword.Text))
{
lblMessagePass.Text = "Password field cannot be empty!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Making sure that password is at least 6 characters long.
else if (txtPassword.Text.Length < 6)
{
lblMessagePass.Text = "Password field must be at least 6 characters long!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Checking for password made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtPassword.Text.Distinct().Skip(1).Any())
{
lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
else
{
passIsValid = true;
lblMessagePass.Text = "Password is valid.";
lblMessagePass.ForeColor = Color.Green;
}
// Making sure that user name and password are not the same.
// Security measure.
if (txtUserName.Text == txtPassword.Text)
{
lblMessageUser.Text = "User Name and Password can not be the same!";
lblMessagePass.Text = "User Name and Password can not be the same!";
lblMessageUser.ForeColor = Color.IndianRed;
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
passIsValid = false;
}
// If all other checks aren't trigered; enable authentication.
if (passIsValid && userIsValid)
{
btnAuthenticate.Enabled = true;
}
}
boolpassisvalid,userIsValid;
公共身份验证窗口()
{
初始化组件();
//创建文本更改事件,直到验证输入字段。
txtextername.TextChanged+=新事件处理程序(txtCheck\u TextChanged);
txtPassword.TextChanged+=新事件处理程序(txtCheck\u TextChanged);
}
私有void txtCheck_TextChanged(对象发送方,事件参数e)
{
//正在检查空用户名字段。
if(string.IsNullOrEmpty(txtUserName.Text))
{
lblMessageUser.Text=“用户名字段不能为空!”;
lblMessageUser.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
}
//确保用户名长度至少为6个字符。
else if(txtUserName.Text.Length<6)
{
lblMessageUser.Text=“用户名字段长度必须至少为6个字符!”;
lblMessageUser.ForeColor=Color.IndianRed;
btnAuthenticate.Enabled=false;
userIsValid=false;
}
//检查用户名