/// <summary> /// Indicates whether the specified bank account number is valid. /// </summary> /// <param name="bank">The bank ID.</param> /// <param name="branch">The bank branch.</param> /// <param name="accountBase">The account base number.</param> /// <param name="suffix">The account suffix.</param> /// <returns><see langword="true"/> if the bank account number is valid, otherwise <see langword="false"/>.</returns> /// <example> /// The following example validates a bank account number using the static method <see cref="IsValid(int, int, int, int)"/>. /// <code> /// var bank = 1; /// var branch = 902; /// var accountBase = 68389; /// var suffix = 0; /// /// var isValid = NZBankAccount.IsValid(bank, branch, accountBase, suffix); /// </code> /// </example> public static bool IsValid(int bank, int branch, int accountBase, int suffix) { var account = new NZBankAccount(bank, branch, accountBase, suffix); var isValid = account.IsValid(); return(isValid); }
private static NZBankAccount ParseThrowIf(string accountNumber, bool throwException) { if (string.IsNullOrEmpty(accountNumber)) { if (throwException) { throw new FormatException("The account number is null or empty."); } return(null); } var match = AccountNumberRegex.Match(accountNumber); if (!match.Success) { if (throwException) { throw new FormatException("The account number is malformed."); } return(null); } var bank = Convert.ToInt32(match.Groups[1].Value); var branch = Convert.ToInt32(match.Groups[2].Value); var accountBase = Convert.ToInt32(match.Groups[3].Value); var suffix = Convert.ToInt32(match.Groups[4].Value); var account = new NZBankAccount(bank, branch, accountBase, suffix); return(account); }
/// <summary> /// Converts the string representation of an account number to its /// <see cref="NZBankAccount"/> equivalent. A return value indicates whether /// the operation succeeded. /// </summary> /// <param name="accountNumber">The string containing an account number to convert, in /// an accepted format.</param> /// <param name="account">When this method returns, a <see cref="NZBankAccount"/> /// equivalent of the specified account number, if the conversion succeeded, or <see langword="null"/> /// if the conversion failed. The conversion fails if the account number is <see langword="null"/>, /// <see langword="Empty"/> or not one of the accepted formats. This parameter is passed uninitialized; /// any value originally supplied in account will be overwritten. /// </param> /// <returns><see langword="true"/> if the conversion succeeded, otherwise <see langword="false"/>.</returns> /// <remarks> /// <para>This creates a new instance of the <see cref="NZBankAccount"/> class, /// but does not validate it. /// </para> /// <para>The accepted formats are: /// <list type="bullet"> /// <item>XX XXXX XXXXXXX XX(X)</item> /// <item>XX-XXXX-XXXXXXX-XX(X)</item> /// <item>XX.XXXX.XXXXXXX.XX(X)</item> /// </list> /// where X represents a digit. The groupings represent the bank, branch, account base and suffix, /// respectively. The suffix can be either 2 or 3 digits. /// </para> /// </remarks> public static bool TryParse(string accountNumber, out NZBankAccount account) { account = ParseThrowIf(accountNumber, false); return(account != null); }
/// <summary> /// Indicates whether the specified bank account number is valid. /// </summary> /// <param name="accountNumber">The string containing an account number to convert, in /// an accepted format.</param> /// <returns><see langword="true"/> if the bank account number is valid, otherwise <see langword="false"/>.</returns> /// <remarks> /// <para>The accepted formats are: /// <list type="bullet"> /// <item>XX XXXX XXXXXXX XX(X)</item> /// <item>XX-XXXX-XXXXXXX-XX(X)</item> /// <item>XX.XXXX.XXXXXXX.XX(X)</item> /// </list> /// where X represents a digit. The groupings represent the bank, branch, account base and suffix, /// respectively. The suffix can be either 2 or 3 digits. /// </para> /// </remarks> /// <example> /// The following example validates a bank account number using the method <see cref="IsValidNZBankAccount(string)"/>. /// <code> /// var account = "26-2600-0320871-32"; /// var isValid = account.IsValidNZBankAccount(); /// </code> /// </example> public static bool IsValidNZBankAccount(this string accountNumber) { var account = NZBankAccount.Parse(accountNumber); return(account?.IsValid() ?? false); }