[Solved] Displaying error message “Email not valid” but still saving to database file


Because all of your code is outside of your if block. So it’s going to execute every time, regardless of the condition.

Put the code in the if block:

Regex regMail = new Regex(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");

if (regMail.IsMatch(mailID.Text))
{
    Email email = new Email();

    string emailAdd = mailID.Text;
    string phone =    phoneBox.Text;
    string messageEmail = email.Text;

    System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\Lia\mail.txt", true);
    file.WriteLine(emailAdd + phone + messageEmail);

    file.Close();
}
else
{
    MessageBox.Show("Invalid Email ID, Please enter \nvalid Email ID.");
}

0

solved Displaying error message “Email not valid” but still saving to database file