Friday, May 1, 2009

Money Validator

I wrote this money (whole dollar) validation method.
It use a regular expression to validate a whole dollar.

public class MoneyValidator
{
/// <summary>
/// Regular expression, which is used to validate an whole dollar (money).
/// </summary>
private const string PATTERN = @"^\$?(((\d{1,3},*)+\d{3})|\d+)$";

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

return false;
}
}

I've been doing alot of unit testing @ work recently. The theory is that everything that you write must be unit tested.
So, I've written this unit test to validate that the function will pass for all whole dollar that I think should be valid and invalid.

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

[TestMethod]
public void ValidMoneyNormal()
{
_money = "$1";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "1";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$10";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "10";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$100";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "100";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$1000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "1000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$1,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "1,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$10000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "10000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$10,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "10,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$100000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "100000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$100,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "100,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$1000000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "1000000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "$1,000,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);

_money = "1,000,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsTrue(result, _money);
}

[TestMethod]
public void InValidMoneyNormal()
{
_money = "$1.99";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "1.99";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$90,999.99";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "90,999.99";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$2..0";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "2..0";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "2,8";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "2,00,0";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "2,00";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$20,00";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "1,0$00,000,000";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$$3";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "2,,,0";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$200$";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$2d";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "$2,,0";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);

_money = "2$3";
result = MoneyValidator.IsValidDollar(_money);
Assert.IsFalse(result, _money);
}
}

Money Validator

Please let me know if this helps you.