Created
September 25, 2012 01:48
-
-
Save jmarnold/3779519 to your computer and use it in GitHub Desktop.
Email Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailAddress : IValidated | |
{ | |
private static readonly Regex EmailRegex = new Regex(@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + | |
@"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? | |
[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." | |
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? | |
[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" | |
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$", RegexOptions.Compiled); | |
public EmailAddress() | |
{ | |
Label = string.Empty; | |
} | |
public EmailAddress(string address) | |
: this() | |
{ | |
Address = address; | |
} | |
public string Label { get; set; } | |
public string Address { get; set; } | |
public void Validate(ValidationContext context) | |
{ | |
if (Address.IsEmpty()) return; | |
if(!EmailRegex.IsMatch(Address)) | |
{ | |
var accessor = ReflectionHelper.GetAccessor<EmailAddress>(x => x.Address); | |
context.Notification.RegisterMessage(accessor, SurgeryLogisticsValidationKeys.INVALID_EMAIL); | |
} | |
} | |
public override string ToString() | |
{ | |
return Address; | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != typeof (EmailAddress)) return false; | |
return Equals((EmailAddress) obj); | |
} | |
public bool Equals(EmailAddress other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(other.Address, Address); | |
} | |
public override int GetHashCode() | |
{ | |
return (Address != null ? Address.GetHashCode() : 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment