验证电子邮件地址 [英] validate e-mail address

查看:166
本文介绍了验证电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有功能或任何快捷方式来验证电子邮件地址(@ sign和.com).我需要检查该邮件地址是否具有域名(例如.com .uk),如果没有给出返回,则大多数返回的示例仅具有@validation

do anyone have function or any shortcut to validate e-mail address (@ sign and .com). I need to check if that mail address is having domain name (like .com .uk) if not give return most of ur examples having only @ validation

推荐答案

使用正则表达式,但是您也可以尝试实例化System.Net.Mail.MailAddress( string address )对象.

构造函数调用内部MailAddressParser类,如果地址非法则抛出异常.

尼克
You could use a Regex, but you can also just try to instantiate a System.Net.Mail.MailAddress( string address ) object.

The constructor calls the internal MailAddressParser class and throws an exception if the address is illegal.

Nick


哇,如果您自己使用google,您会得到更快的结果!

Wow, you would have got a quicker result if you had just used google yourself!

public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
{
return false;
}

int nFirstAT = sEmail.IndexOf('@');
int nLastAT = sEmail.LastIndexOf('@');

if ( (nFirstAT > 0) && (nLastAT == nFirstAT) &&
(nFirstAT < (sEmail.Length - 1)) )
{
// address is ok regarding the single @ sign
return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
}
else
{
return false;
}
}



从这里开始(那里还有其他示例!);
http://bytes.com/topic/c- sharp/answers/225262-code-validating-email-address-c-validate-email [



from here (there is other examples out there also!);
http://bytes.com/topic/c-sharp/answers/225262-code-validating-email-address-c-validate-email[^]


我绝对不喜欢使用正则表达式,但这确实是在这种情况下,最适合工作的工具:


I absolutely abhor using regex, but it really is the best tool for the job in this case:


//--------------------------------------------------------------------------------
public static bool ValidEmailAddress(string addy)
{
    bool valid = true;
    string pattern = @"^[a-z0-9!#


这篇关于验证电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆