33 lines
872 B
C#
33 lines
872 B
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace IdentityService.Domain.ValueObjects
|
|
{
|
|
public class Email
|
|
{
|
|
public string Value { get; }
|
|
public Email(string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
throw new ArgumentNullException("邮箱地址不可为空");
|
|
}
|
|
|
|
string pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
|
|
if (!Regex.IsMatch(value, pattern))
|
|
{
|
|
throw new ArgumentException("邮箱地址格式不符合要求");
|
|
}
|
|
this.Value = value.ToLowerInvariant();
|
|
}
|
|
public override string ToString()
|
|
{
|
|
return Value;
|
|
}
|
|
|
|
public static implicit operator Email(string value)
|
|
{
|
|
return new Email(value);
|
|
}
|
|
}
|
|
}
|