• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjQ1

Related Code
Url Validation with Regular Expression
Generic Entity Framework 4.0 Base Repository
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
Generic Entity Framework 4.0 Base Repository with Paging
Generic linq-to-sql repository
WCF RIA Services Unity 2.0 DomainService Factory
A Default Entity Framework 4.0 ObjectContext Factory
UnitOfWork Action filter for ASP.NET MVC and nHibernate
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Custom C# exception class
Reflection dynamically get private fields or properties
Method to hash passwords
Number of sealed / unsealed types in the framework
Extension to the HttpClient

Email validation with Regular Expression

Validates if an e-mail is in a correct format

0
579 0 0 0 0 4

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);
    }
}

Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

regular-expression

Mark 'regular-expression' tag as 'like'

Mark 'regular-expression' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
September 11, 2010 7:12 PM
edited September 14, 2010 10:13 PM

Fork

 Email validation with Regular Expression -  @fredrikn Saturday 11, 2010 7:12 PM


0 Feedback


You must log in before you can give any feedback


4 Discussion(s)

Newest Oldest
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;
    }
link | flag  | Reply

 Jiggr "Code Contributor"
123
Tuesday 14, 2010 10:03 PM

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;
        }
    }
}
link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Tuesday 14, 2010 10:15 PM

0

Nice beaver!

Do you implement a SendEmail-method in that class? Or do you have your Email class so simple?

link | flag  | Reply

 Jiggr "Code Contributor"
123
Tuesday 14, 2010 10:34 PM

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"..

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Wednesday 15, 2010 7:06 AM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010