gpt4 book ai didi

c# - 当我输入第一个字符时,C#-确认,密码和电子邮件文本框显示我消息框

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:44 25 4
gpt4 key购买 nike

enter image description here

 //code for confirm password text box:

private void txtRegPassConfirm_TextChanged_1(object sender, EventArgs e)
{
if (txtRegPassConfirm.Text != txtRegPass.Text)
{
MessageBox.Show("Passwords do not match");
txtRegPassConfirm.Clear();
}

else
{
MessageBox.Show("textbox can not be empty");
}
}


//code for text box Password:

private void txtRegPass_TextChanged(object sender, EventArgs e)
{

if (txtRegPass.Text.Length < 8)
{

MessageBox.Show("Password must be at least 8 characters long");
txtRegPassConfirm.Clear();
}


// code for text box Email:

private void txtRegEmail_TextChanged_1(object sender, EventArgs e)
{


string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("EmailReg", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email", this.txtRegEmail.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
MessageBox.Show("Email = " + dr[4].ToString() + "is Already exist");
txtRegEmail.Clear();
break;
}
Regex r = new Regex("^[a-zA-Z0-9){1,20}@[a-zA-Z0-9){1,20}.[a-zA-Z]{2,3}$");

if (!r.IsMatch(txtRegEmail.Text))
{
txtRegEmail.Clear();
MessageBox.Show("incorrect formate");
}

}

}

//code for button Registration:

private void btnRegistration_Click_1(object sender, EventArgs e)
{

string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("RegistrationForm", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", txtRegUserN.Text);
cmd.Parameters.AddWithValue("@Password", txtRegPass.Text);
cmd.Parameters.AddWithValue("@Confirm", txtRegPassConfirm.Text);
cmd.Parameters.AddWithValue("@Email", txtRegEmail.Text);
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
if (dr.HasRows == true)
{
MessageBox.Show("Data Inserted Succesfully");

}
}

if (dr.HasRows == false)
{

MessageBox.Show("Access Denied, enter the correct fields");
}

else
{
MessageBox.Show("Enter correct info");
}
}


当我执行该应用程序并在确认文本框中输入密码时,它会向我显示输入第一个字符“密码不匹配”的消息框。以及在“密码”文本框中显示“密码必须至少8个字符长”的消息。就像在“电子邮件”文本框中一样,我要应用正则表达式但不起作用。我将代码移到了电子邮件部分,但是它显示了“电子邮件无效”正则表达式消息框。现在,告诉我输入不匹配字(而不是输入第一个字符)时如何获得消息框。

最佳答案

主要问题:

您正在检查文本框TextChanged事件中的验证规则。下面列出了一些可以帮助您执行验证的选项:

选项1:在按钮的单击事件中验证

您可以在注册按钮的Click事件中检查验证,例如:

private void registerButton_Click(object sender, EventArgs e)
{
//Check for password length
if (txtRegPass.Text.Length < 8)
{
MessageBox.Show("Password must be at least 8 characters long");
txtRegPass.Focus();
return;
}

//Check for other validations
//...
// don't forget to return; if the state is not valid


//If the code execution reaches here, it means all validation have been passed
//So you can save data here.
}


选项2:使用验证事件和ErrorProvider

作为更好的解决方案,可以使用 ErrorProvider。将错误提供程序放在窗体中并处理TextBoxes的 Validatin事件,然后为该控件设置错误。

然后在您的注册按钮的Click事件中,您可以检查是否存在任何验证错误。

这是屏幕截图:

enter image description here

private void ValidationTest_Load(object sender, EventArgs e)
{
this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
}

private void passwordTextBox_Validating(object sender, CancelEventArgs e)
{
if (this.passwordTextBox.TextLength < 8)
{
this.errorProvider1.SetError(this.passwordTextBox, "Password must be at least 8 character");
e.Cancel = true;
}
else
{
this.errorProvider1.SetError(this.passwordTextBox, "");
}
}

private void confirmTextBox_Validating(object sender, CancelEventArgs e)
{
if (this.confirmTextBox.Text != this.passwordTextBox.Text)
{
this.errorProvider1.SetError(this.confirmTextBox, "Password and Confirm must be the same");
e.Cancel = true;
}
else
{
this.errorProvider1.SetError(this.confirmTextBox, "");
}
}

private void registerButton_Click(object sender, EventArgs e)
{
if (this.ValidateChildren())
{
//Do registration here
}
else
{
var listOfErrors = this.errorProvider1.ContainerControl.Controls
.Cast<Control>()
.Select(c => this.errorProvider1.GetError(c))
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
MessageBox.Show("please correct validation errors:\n - " +
string.Join("\n - ", listOfErrors.ToArray()),
"Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

关于c# - 当我输入第一个字符时,C#-确认,密码和电子邮件文本框显示我消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com