This is a library to validate IRD numbers and New Zealand bank account numbers according to the "Resident Withholding Tax (RWT) and Non-Resident Withholding Tax (NRWT)" specification published by the New Zealand Inland Revenue Department (IRD).
The specification used in this implementation is here.
This library is distributed with Nuget. The package can be installed by:
Install-Package Spectrum.Ird
Import the namespace.
using Spectrum.Ird;
To validate an IRD number:
var isValid = 49091850.IsValidIrdNumber();
To validate a New Zealand bank account number:
var isValid = "01-0902-0068389-00".IsValidNZBankAccount();
Create an instance of the IrdNumber
class with an IRD Number expressed as a long
data type. Call the IsValid()
method to validate it.
var irdNumber = new IrdNumber(49091850);
var result = irdNumber.IsValid();
After an IRD number has been instantiated, the ToString()
method can be called to get a friendly display of the IRD number, hyphen separated.
// arrange
var irdNumber = new IrdNumber(49091850);
// act
var result = irdNumber.ToString();
// assert
Assert.AreEqual("49-091-850", result);
A static method of validation is available.
var isValid = IrdNumber.IsValid(49091850);
Create an instance of the NZBankAccount
with a bank account number in its constituent parts. That is, its bank, branch, account base and suffix. Call the IsValid()
method to validate it.
var bank = 1;
var branch = 902;
var accountBase = 68389;
var suffix = 0;
var account = new NZBankAccount(bank, branch, accountBase, suffix);
var isValid = account.IsValid();
An instance can also be created by parsing a string
value in the format XX-XXXX-XXXXXXX-XX(X)
where X
is a digit and the suffix can be either 2 or 3 digits. Hyphens, spaces or periods can be used as separators. If the value cannot be parsed, a FormatException
is thrown.
Parsing an account number does not validate it. Once an instance has been created, it can then be validated.
try
{
var account = NZBankAccount.Parse("01-0902-0068389-00");
var isValid = account.IsValid();
}
catch (FormatException ex)
{
// handle exception
}
A TryParse()
method is also available.
if (NZBankAccount.TryParse("01-0902-0068389-00", out NZBankAccount account))
{
var isValid = account.IsValid();
}
After an account number has been instantiated, the ToString()
method can be called to get a friendly display of the account number, hyphen separated.
// arrange
var account = new NZBankAccount(1, 902, 68389, 0);
// act
var result = account.ToString();
// assert
Assert.AreEqual("01-0902-0068389-00", result);
A static method of validation is available.
var isValid = NZBankAccount.IsValid(1, 902, 68389, 0);
2020-10-21
- Override ToString() to output a human-friendly formatted IRD number.
2020-10-04
- Added exception XML documentation for the
Parse
method. - Added ExpectedExceptionWithMessage for unit testing.
2020-09-27
- Aligned how the
Parse
method works with typical Microsoft parsing methods such asInt32.Parse(string)
. An exception will now be thrown if the value cannot be parsed instead of returningnull
. - Added extension methods.
2020-09-26
- Added IRD Number validation.
- Refactored
NZBankAccount
to use integer arrays. - Removed
Algorithm
andModulus
as these were never populated.
2020-09-21
- Changed the namespace from
Spectrum.IrdValidation
toSpectrum.Ird
. - Renamed
NZBankAccountValidator
toNZBankAccount
. - Removed AccountNumber property as it is misleading and could be confused with ToString(). This is used privately as part of the validation algorithm.
- Added Parse() to accept bank account numbers in a "XX-XXXX-XXXXXXX-XX(X)" format.
2020-09-17
- Override ToString() to output a human-friendly formatted account number.
2020-09-12
- Added XML documentation.
2020-09-06
- Initial release