开发者

format string email to email-link

开发者 https://www.devze.com 2022-12-13 21:57 出处:网络
I have a nullable-string email, and I would like to format the string as a mailto-link if it seems valid.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消