Reputation:
//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");
}
}
When I execute the application and enter the password in a confirm text box, it shows me the message box on entering the first character 'Password don't match'. As well as shows me message in the Password text box 'Password must be at least 8 characters long'. And as same as in the Email text box and I want to apply the Regex but not working. I moved my code in the email section but, it shows me the 'Email is invalid' regex message box. Now, tell me what I can do get the message box when I enter the mismatch words not on entering the first character.
Upvotes: 1
Views: 6546
Reputation: 125197
Main Problem:
You are checking your validation rules in TextChanged
event of your text boxes. Some options that may help you to perform validation is listed here:
Option1: validate in Click Event of Button
You can check for validations in Click
event of your register button some thing like this:
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.
}
Option 2: Use Validating Event and ErrorProvider
As a better solution, you can use an ErrorProvider
. Put an error provider in form and handle Validatin
event of your TextBoxes and set error for that control.
Then in Click event of your register button you can check if there is any validation error or not.
and here a screenshot:
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);
}
}
Upvotes: 1
Reputation: 196
The validation can also be done while moving to the next text box(Validation of previous text box input) by pressing tab or by mouse click. This can be done in the textbox events Leave or Enter.
private void textBox1_Leave(object sender, EventArgs e) // This event is triggered while leaving the textbox1
{
if ("a" != textBox1.Text)
{
MessageBox.Show("Invalid");
}
}
private void textBox2_Enter(object sender, EventArgs e)// This event is triggered while entering the textbox2
{
if ("a" != textBox1.Text) //Content of textbox1 is validated
{
MessageBox.Show("Invalid");
}
}
Upvotes: 1
Reputation: 12085
The problem is you put your validation code in txtRegPassConfirm_TextChanged_1
function. As the name of the function said, that function will be triggered whenever the text in txtRegPassConfirm
textbox changed. That's why you get the MessageBox
when you first type.
To solve your problem, put your validation code in the Click event
of Register button
instead of TextChanged
event of each textbox.
Another better solution is: use Error Provider Control
to show your error instead of using MessageBox
. To know more about it, take a look at MSDN.
Upvotes: 1
Reputation: 297
Here is your solution
private void btnRegistration_Click_1(object sender, EventArgs e)
{
if (txtRegPassConfirm.Text != txtRegPass.Text)
{
MessageBox.Show("Passwords do not match");
txtRegPassConfirm.Clear();
return;
}
else
{
if (txtRegPass.Text.Length < 8)
{
MessageBox.Show("Password must be at least 8 characters long");
txtRegPassConfirm.Clear();
return;
}
}
//put your next code as it is which you wrote in this click event
}
Thanks
Upvotes: 1
Reputation: 12439
Because you are using TextChanged
event and it raises every time you change the Text
. You should check this on click of your button Register
.
Not only this one, your all validations should be checked on the button click.
For the help of user, you can use a Label
to show the the message while user is typing something, rather showing a MessageBox
. That will be a smooth experience for user.
Upvotes: 1