【问题标题】:Regular expression to cancel event if incorrect如果不正确则取消事件的正则表达式
【发布时间】:2016-08-04 21:13:56
【问题描述】:

我在 c# winforms 中有一个正则表达式。我有它来确定格式是否正确。如果正确,它将继续保存,但如果正确,它将显示其验证并取消保存事件。

验证码

private void emailTxt_Validating(object sender, CancelEventArgs e)
{
    string pattern = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

    if (Regex.IsMatch(emailTxt.Text, pattern))
    {
        e.Cancel = false;
    }
    else
    {
        e.Cancel = true;
        errorEmail.SetError(emailTxt, "Incorrect Format Try... email@email.com");
        CancelBtn.Enabled = true;              
    }
}

保存代码

private void SaveBtn_Click(object sender, EventArgs e)
        {
            //SQL Connection and SQL for inserting a new admin
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
            con.Open();

            if (ValidateChildren(ValidationConstraints.Enabled))
            {
                try
                {
                    string sql = "INSERT INTO Admin (Admin_Username, Admin_FName, Admin_SName, Admin_Email, Admin_Password) " + "VALUES (@adminName, @adminFirstname, @adminSurname, @adminEmail, @adminPassword);";

                    using (var cmd = new SqlCommand(sql, con))
                    {
                        cmd.Parameters.AddWithValue("@adminName", usernameTxt.Text);
                        cmd.Parameters.AddWithValue("@adminFirstname", firstnameTxt.Text);
                        cmd.Parameters.AddWithValue("@adminSurname", surnameTxt.Text);
                        cmd.Parameters.AddWithValue("@adminEmail", emailTxt.Text);
                        cmd.Parameters.AddWithValue("@adminPassword", passwordTxt.Text);
                        cmd.ExecuteNonQuery();
                    }

                    MessageBox.Show("Admin Successfully Added!");

                    this.Close();

                }

                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);


                }
            }

            con.Close();
        }

它表明它的格式不正确,但即使格式不正确,它也会继续保存事件。

【问题讨论】:

  • 错误在其他地方,我们看不到。什么是保存代码?
  • @GiorgiNakeuri 我已经用我的保存代码更新了上面的代码。
  • 你能不能显示validateChildren方法
  • @Valentin 以上是全部代码。
  • @DonaldBury,应该是 ValidateChildren 方法。你应该展示它。

标签: c# regex winforms


【解决方案1】:

我已经通过将它嵌套在已经验证其是否为空的函数中来使其工作。这似乎有效。

private void emailTxt_Validating(object sender, CancelEventArgs e)
    {

    if (string.IsNullOrWhiteSpace(emailTxt.Text))
    {
        e.Cancel = true;
        errorEmail.SetError(emailTxt, "Email is Empty!");
        CancelBtn.Enabled = true;
    }
    else
    {
        string email = emailTxt.Text;
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        Match match = regex.Match(email);

        if (match.Success)
        {
            e.Cancel = false;
        }
        else
        {
            e.Cancel = true;
            errorEmail.SetError(emailTxt, "Incorrect Format Try... email@email.com");
            CancelBtn.Enabled = true;
            return;
        }                                
    }
}

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多