Tuesday, September 16, 2008

Email Address Validator

I wrote this email address validation method a few months ago.
It use a regular expression to validate an email address.

public class EmailAddressValidator
{
/// <summary>
/// Regular expression, which is used to validate an email address.
/// </summary>
private const string PATTERN = @"^[a-z|0-9][a-z|0-9]*([_'-][a-z|0-9]+)*" +
@"(([.][a-z|0-9]+)+([_'-][a-z|0-9]+)*)?@" +
@"[a-z|0-9][a-z|0-9]*([-][a-z|0-9]+)*" +
@"(([.][a-z|0-9]+)+([-][a-z|0-9]+)*)$";

/// <summary>
/// Checks whether the given email address is valid.
/// </summary>
/// <param name="emailAddress">String that contains an email address.</param>
/// <returns>True, if valid email address; otherwise false.</returns>
public static bool IsValidEmailAddress(string emailAddress)
{
if (!string.IsNullOrEmpty(emailAddress))
return Regex.IsMatch(emailAddress, PATTERN, RegexOptions.IgnoreCase);
else
return false;
}
}

I wrote this unit test to validate that the function will pass for all email that I think should be valid and invalid.

/// <summary>
/// Summary description for UnitTest
/// </summary>
[TestClass]
public class UnitTest
{
string _emailAddress;
bool _result;

[TestMethod]
public void ValidEmailAddressNormal()
{
_emailAddress = "a@b.c";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe@home.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe.bob123@home.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "123joe@home.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe.bob@home.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe.bob@his.home.place";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "duke_me.baby@hotmail.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "o'reilly@there.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "uat-test.me@hotmail123.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe_bob@jb-test.com.au";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "duty.it.sit.dev.prog@test.com.au";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe.bob@google.it.com.au";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);

_emailAddress = "joe.bob@123.test.me.co.uk";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsTrue(result, _emailAddress);
}

[TestMethod]
public void InValidEmailAddressNormal()
{
_emailAddress = "joe";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe@home";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe-bob[at]home.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe.@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = ".joe@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe@.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe..bob@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe<>bob@bob.come";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe~bob@bob.come";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe&bob@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "~joe@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe$bob@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe+bob@bob.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe_bob@hotmail..com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "joe_bob@hotmail...com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "email withspace@hotmail.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "test@hot_mail.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);

_emailAddress = "test@o'reilly.com";
result = EmailAddressValidator.IsValidEmailAddress(_emailAddress);
Assert.IsFalse(result, _emailAddress);
}
}

Email Address Validator

Please let me know if this helps you.

Thursday, September 11, 2008

WPF drop down and split button

I've been trying to create a drop down button and a split button in WPF.
It has been very frustrating, but I think I finally got it.

Drop Down and Split Button ver 1.2

I have updated the split button control to use command binding.

Please let me know if this helps you.