Reputation: 9298
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:[email protected]">[email protected]</a>
How is that done?
/M
Upvotes: 1
Views: 2985
Reputation: 9726
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 "";
}
Upvotes: 5
Reputation: 34195
string formatIfValid(string email) {
if(!validEmail(email))
return null;
return "<a href=\"mailto:" + email + "\">" + email + "</a>";
}
Or did you ask about something else really?
Upvotes: 3
Reputation: 40789
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.
Upvotes: 3