C#电子邮件验证与if语句 [英] c# email validation with if statement
本文介绍了C#电子邮件验证与if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何确保@和.之间至少有两个字符. character
how do you make sure there is atleast two characters between the @ and the . character
推荐答案
示例:
Hi,
Example:
string emailAddr = "MyEmail@live.com";
bool isValid = IsEMailAddrValid(emailAddr);
if (isValid)
{
// popup message box?
}
// Do anything ?
private bool IsEMailAddrValid(string emailAddr)
{
bool result = true;
int count = emailAddr.Count(f => f == '@');
if (count == 0)
{
return false;
}
if (count > 1)
{
return false;
}
count = emailAddr.Count(f => f == '.');
if (count == 0)
{
return false;
}
if (count > 1)
{
return false;
}
string theChar = emailAddr.Split('@')[1].ToString();
int theCharLen = theChar.Split('.')[0].ToString().Length;
if (theCharLen < 2)
{
result = false;
}
return result;
}
Regards,
使用此类
use this class
using System.Text.RegularExpressions;
public static class Validator
{
static Regex ValidEmailRegex = CreateValidEmailRegex();
/// <summary>
/// Taken from http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
/// </summary>
/// <returns></returns>
private static Regex CreateValidEmailRegex()
{
string validEmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ @"([-a-z0-9!#
%& ;'* +/=?^ _`{|}〜] |(?<!\.)\.)*)(?<!\.)" + @"
%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)" + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]
这篇关于C#电子邮件验证与if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文