public static class EmailValidation
{
public static bool IsValidEmail(string value)
{
if (String.IsNullOrWhiteSpace(value ))
threw new ArgumentNullException("Validation.IsEmail's value can't be null or empty");
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
return re.IsMatch(value);
}
}
Fork
0 Feedback
You must log in before you can give any feedback
0
I got tired of the regexps to email adresses because I didnt find a good one. Maybe the one you wrote is good enough. I will try it later.
Instead I implemented this dirty code that works perfect!
public bool IsEmail(string value)
{
try
{
MailAddress mailAddress = new MailAddress(value);
}
catch
{
return false;
}
return true;
}
1
Reply to: Jiggr
In my current project I have created an custom class called Email, where the validation takes place. So instead of an string I use Email, so I don't need to add validations to each "Email" property of type string. Something like this:
public class Customer
{
public Email Email { get; set; }
...
}
public class Email
{
private string _email;
protected Email() { }
public Email(string email)
{
Value = email;
}
public virtual string Value
{
get { return _email; }
set
{
EmailValidation.IsValidEmail(value);
_email = value;
}
}
}
0
Nice beaver!
Do you implement a SendEmail-method in that class? Or do you have your Email class so simple?
0
Reply to: Jiggr
I have it simple, but I also inherits a base class called ValueObject, to override the equal behavior, so when I compare two e-mails with each other, it will not look at the reference, instead it will compare with the value. customer.Email == "info@something.com"..
You must log in before you can post a comment


579
0


Mark '.net' tag as 'like'
Mark '.net' tag as 'ignore'