I am working on validation for a WPF application and here is my class to validate American Zip Codes.

class ZipCodeRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
ErrorMessage errMessage = new ErrorMessage();
Docs.App_Code.Validation validationHelper = new Docs.App_Code.Validation();
try
{
string currentValue = (string)value;
if (currentValue.Length == 5)
{
if (validationHelper.IsNumeric(currentValue))
{
return ValidationResult.ValidResult;
}
}
else if (currentValue.Length == 9)
{
if (validationHelper.IsNumeric(currentValue))
{
return ValidationResult.ValidResult;
}
}
else if (currentValue.Length == 10)
{
string firstFive = currentValue.Substring(0, 5);
string lastFour = currentValue.Substring(6, 4);
if (validationHelper.IsNumeric(firstFive)) {
if (validationHelper.IsNumeric(lastFour)) {
if (currentValue.Substring(5,1) == “-“) {
return ValidationResult.ValidResult;
}
}
}
}
else if (currentValue.Length == 0)
{
return ValidationResult.ValidResult;
}
return new ValidationResult(false, errMessage.getErrorMessage(1005));
}
catch (FormatException)
{
return new ValidationResult(false, errMessage.getErrorMessage(1005));
}
}
}