C# 如何跳过一个条件?

C# 如何跳过一个条件?,c#,.net,regex,validation,if-statement,C#,.net,Regex,Validation,If Statement,我想更新供应商信息,并想删除文本框txtWeb的内容 然而,当我调试它时,它总是进入我的正则表达式的条件来验证txtWeb文本框的内容,即使文本框的内容是空的。当我在文本框中输入正确的URL时,它工作正常,但在没有内容的情况下它不工作 但是,数据库表允许txtWeb列中为null 问题出现在最后一个“如果”条件下 public void Modifier_Supplier() { SamsonEntities db = new SamsonEntities

我想更新供应商信息,并想删除文本框txtWeb的内容

然而,当我调试它时,它总是进入我的正则表达式的条件来验证txtWeb文本框的内容,即使文本框的内容是空的。当我在文本框中输入正确的URL时,它工作正常,但在没有内容的情况下它不工作

但是,数据库表允许txtWeb列中为null

问题出现在最后一个“如果”条件下

public void Modifier_Supplier()
        {
            SamsonEntities db = new SamsonEntities();
            try
            {
                action = "MODIF";
                if (gridSuppliers.CurrentRow != null && gridSuppliers.CurrentRow.DataBoundItem != null)
                {
                    SupplierDisplay supAct = (SupplierDisplay)gridSuppliers.CurrentRow.DataBoundItem;

                    Supplier supUpd = db.Suppliers.Single(sup => sup.SupplierID == supAct.SupplierID);

                    supUpd.SupplierName = txtNom.Text;
                    supUpd.Address = txtAdresse.Text;
                    supUpd.City = txtVille.Text;
                    supUpd.PostalCode = mskPostal.Text;
                    supUpd.Contact = txtContact.Text;
                    Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    Match match = regex.Match(txtCourriel.Text);
                    if (!match.Success)
                    {
                        MetroMessageBox.Show(this, "Ce courriel est invalide", "Message d'erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        supUpd.Email = null;
                        return;
                    }
                    else
                        supUpd.Email = txtCourriel.Text;
                    supUpd.Phone = mskPhone.Text;
                    Regex regexWeb = new Regex(@"^(https?://)?([\da-z.-]+)\.([a-z\.]{2,6})([/\w .-]*)/?$");
                    Match matchWeb = regexWeb.Match(txtWeb.Text);
                    if (!matchWeb.Success)
                    {
                        MetroMessageBox.Show(this, "Ce site web est invalide", "Message d'erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        supUpd.Website = null;
                        return;
                    }
                    else
                        supUpd.Website = txtWeb.Text;

                    db.SaveChanges();

                    FillSuppliers();

                    MetroMessageBox.Show(this, "Fournisseur mis à jour", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }

我理解您的问题,当txtWeb文本框中没有文本时,您不需要正则表达式验证。为此,只需添加一个if条件来检查txtWeb是否为空

if(txtWeb.Text != String.Empty)
  Match matchWeb = regexWeb.Match(txtWeb.Text);

我不太明白。如果您说字段可以为空,为什么不添加一个或条件以允许“”?什么?我不明白你的评论?我的评论说的正是有人刚刚发布的答案。检查一个空字符串并允许我做的就是用您的建议替换我的上一个条件?为什么添加ASP.NET的标记?这个Q不属于ASP.NET吗?是Windows应用程序吗?此应用程序中没有ASP.NET,它是Windows应用程序