Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何使用c在自定义验证器中包含正则表达式#_Asp.net_Regex_Validation - Fatal编程技术网

Asp.net 如何使用c在自定义验证器中包含正则表达式#

Asp.net 如何使用c在自定义验证器中包含正则表达式#,asp.net,regex,validation,Asp.net,Regex,Validation,我正在使用自定义验证器,这样我可以设置文本框的样式。但是,我不确定如何验证电子邮件地址-可能使用正则表达式?以下是我试图修改的代码: protected void CustomValidatorBillEmail_ServerValidate(object sender, ServerValidateEventArgs args) { bool is_valid = txtBillingEmail.Text != ""; txtBillingEmail.B

我正在使用自定义验证器,这样我可以设置文本框的样式。但是,我不确定如何验证电子邮件地址-可能使用正则表达式?以下是我试图修改的代码:

protected void CustomValidatorBillEmail_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        bool is_valid = txtBillingEmail.Text != "";
        txtBillingEmail.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
        args.IsValid = is_valid;
    }

我们将一如既往地感谢您提供的任何帮助。

.Net framework附带了一个正则表达式类(System.Text.RegularExpressions),然后您将提供一个正则表达式来验证电子邮件在网络上有100个版本

以下是我的解决方案:

protected void CustomValidatorBillEmail_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
         @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
         @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

        Regex regex = new Regex(strRegex);

        bool is_valid = false;

        if (regex.IsMatch(txtBillingEmail.Text))
        { 
            is_valid = true; 
        }

        txtBillingEmail.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
        args.IsValid = is_valid;
    }

可以改进吗?需要学习

可能是@Jacob Eggers的副本谢谢你的提示。。。