I have this method to validate email addresses:
public static bool isEmail(string inputEmail)
{
inputEmail = NulltoString(inputEmail);
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 re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
But I get the error: The name 'NulltoString' does not exist in the current context
.
Is there a string
method that is equivalent to NulltoString()
?
C#语言已经有了一个很好的功能, null-coalescing运算符 :
inputEmail = inputEmail ?? string.Empty;
尝试使用以下内容:
inputEmail = inputEmail ?? String.Empty;
I suggest
public static bool isEmail(string inputEmail)
{
inputEmail = inputEmail?? string.Empty;
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 re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
More efficient than that:
if (null == inputEmail)
return false;
You could try
if(string.IsNullOrEmpty(inputEmail))
//throw exception
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.