Main | links of the day »

ABN validator in C#

public static bool ABNIsValid(string abn)
//caller is responsible for stripping any spaces before calling
//per http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm
// via http://www.clearwater.com.au/?action=code
{

  if (!System.Text.RegularExpressions.Regex.Match(abn,@"^\d{11}$").Success) return false;
  int[] weights = new int[] {10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
  int sum = 0;
  for (int i=0;i<11; i++) {
    int digit = int.Parse(abn[i].ToString());
    if (i==0) {digit--;}
    sum += weights[i] * digit;
  }
  return ((sum % 89) == 0);
}