I have a nullable-string email, and I would like to format the string as a mailto-link if it seems valid.
like this:
<a href="mailto:foo@foo.com">foo@foo.com</a>开发者_高级运维;
How is that done?
/M
public string emailLink(string emailAddress)
{
Regex emailRegex = new Regex(@"^(?!.*\.\.)[a-zA-Z0-9\w\._%&!'*=?^+-]*@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
if (emailRegex.IsMatch(emailAddress)
{
return string.Format("<a href=\"mailto:{0}\">{0}</a>", emailAddress);
}
return "";
}
var link = IsValid(email)
? string.Format("<a href='mailto:{0}'>{0}</a>", email)
: email
where function IsValid
is implemented in whichever way meets your needs.
string formatIfValid(string email) {
if(!validEmail(email))
return null;
return "<a href=\"mailto:" + email + "\">" + email + "</a>";
}
Or did you ask about something else really?
精彩评论