简体   繁体   English

如何拆分文本框值并检查它是c#中的电子邮件ID还是纯文本

[英]How to split text box value and check whether it is email ID or plain text in c#

I have a text box(txt_to) which has values seperated by ; 我有一个文本框(txt_to),其值分隔; How can I split the values, and even if there is only one value the code should not through error. 如何拆分值,即使只有一个值,代码也不应该通过错误。

I tried to split it with the following code but got an error 我尝试使用以下代码拆分它但出错了

string[] recipients = Request.Form["txt_to"].Split(';');
Object reference not set to an instance of an object.

The values in the text box are either a email like (abc@aaa.com) or just a plain text like (Ios Group). 文本框中的值可以是电子邮件(abc@aaa.com),也可以是纯文本(Ios Group)。 the values in the text box are like abc@aaa.com; 文本框中的值类似于abc@aaa.com; Ios Group. Ios集团。 How to split the value and how to check whether it is email id or just a plain text 如何拆分值以及如何检查它是电子邮件ID还是纯文本

if(email id)
{
Do this
}
else if (Plain text)
{
Do this
}

RFC 2822 states that you can validate an e-mail with the following regular expression: RFC 2822声明您可以使用以下正则表达式验证电子邮件:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

So you can do: 所以你可以这样做:

if (Regex.IsMatch(input, **REGEX STRING HERE**))
{
    // E-mail
}
else
{
    // Not an e-mail
}

string.Split doesn't throw an error when you split a string using a character that is not present in the string. 使用字符串中不存在的字符拆分字符串时,string.Split不会引发错误。

Example: 例:

string test =  "jiberish";

string [] result = test.Split(';');
Console.WriteLine(result[0]);   

Won't blow. 不会吹。 If you get a null reference exception is because Request.Form["txt_to"] is null 如果得到空引用异常是因为Request.Form["txt_to"]为空

If you want to validate email addresses, use a Regular expression. 如果要验证电子邮件地址,请使用正则表达式。

Something like: 就像是:

if(Regex.IsMatch("\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b",yourstring))
{
   //valid email
}
else
{
   //not valid
}

Note: above regular expression may not be the most appropriate. 注意:上面的正则表达式可能不是最合适的。 I used it to give you an idea. 我用它来给你一个想法。

You should be able to use aa RegularExpressionValidator for this: 你应该可以使用一个RegularExpressionValidator

<asp:RegularExpressionValidator ID="regEmail" runat="server" Display="Dynamic" ErrorMessage="*" ValidationExpression="^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$" ControlToValidate="txtEmail" />                                    

Once implemented, you can verify that the field(s) are valid in code-behind like this: 实现后,您可以验证字段在代码隐藏中是否有效,如下所示:

bool isValid = Page.IsValid;

If you don't want to use validators, you can just use an expression in code-behind to validate the email address like this: 如果您不想使用验证器,则可以使用代码隐藏中的表达式来验证电子邮件地址,如下所示:

var regex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
bool isValid = regex.IsMatch(txtEmail.Text);

Here are a couple of expressions you can try out: 您可以尝试以下几种表达方式:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$

You can simply do : 你可以简单地做:

if the string contains an "@" than try to build a MailAdress object (in a try/catch) if it doesn't it's just plain text :) 如果字符串包含“@”而不是尝试构建一个MailAdress对象(在try / catch中),如果它不是它只是纯文本:)

if(!string.IsNullOrEmpty(textToTest){
    try{
       mail = new MailAdress(textToTest);
    }catch(Exception e){
       plainText = textToTest
    }
}

EDIT : Update in order to take into account comments :) 编辑:更新,以考虑到评论:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM