Click here to Skip to main content
16,006,605 members
Articles / Programming Languages / Javascript

ABN Validator for Australia

Rate me:
Please Sign up or sign in to vote.
1.75/5 (3 votes)
24 Aug 2020CPOL 7.3K   1   6
Simple function to validate the ABN for Australian's trade
Here, you will see a validator to calculate if the ABN number is correct or not.

Introduction

In this post, you will see a simple function to validate the ABN for Australian's trade. This validator is for calculating if the ABN number is correct or not. I create a function in JavaScript and C#. I hope this will help you in some way.

C# Version

C#
/// Check if the ABN number is correct
/// number to check.
public static bool Abn_Validator(string ABN)
{
bool result;
ABN = ABN.Trim().Replace(" ", "");
if (ABN.Length == 11)
{
try            //this try cach is because if user try to send 123A567890B get exception
{
char[] nums = ABN.ToArray();
int[] control = new int[11];
control[0] = (Convert.ToInt32(nums[0].ToString()) - 1) * 10;
control[1] = Convert.ToInt32(nums[1].ToString());
control[2] = Convert.ToInt32(nums[2].ToString()) * 3;
control[3] = Convert.ToInt32(nums[3].ToString()) * 5;
control[4] = Convert.ToInt32(nums[4].ToString()) * 7;
control[5] = Convert.ToInt32(nums[5].ToString()) * 9;
control[6] = Convert.ToInt32(nums[6].ToString()) * 11;
control[7] = Convert.ToInt32(nums[7].ToString()) * 13;
control[8] = Convert.ToInt32(nums[8].ToString()) * 15;
control[9] = Convert.ToInt32(nums[9].ToString()) * 17;
control[10] = Convert.ToInt32(nums[10].ToString()) * 19;

int total = 0;
for (int i = 0; i < 11; i++)
{
total += control[i];
}

if (total % 89 == 0) result = true;
else result = false;
}
catch
{
result = false;
}
}
else result = false;

return result;
}

JavaScript Version

JavaScript
function Abn_Validator(myAbn)
    {
        let result = false;
        let ABN;
        try {
            ABN = myAbn.value;
        } catch (e) {
            ABN = '';
        }
        if (ABN.length == 11) {
            try {
                let control = new Array(11);
                control[0] = (convert2integer(ABN[0],false) - 1) * 10;
                control[1] = convert2integer(ABN[1], false);
                control[2] = convert2integer(ABN[2], false) * 3;
                control[3] = convert2integer(ABN[3], false) * 5;
                control[4] = convert2integer(ABN[4], false) * 7;
                control[5] = convert2integer(ABN[5], false) * 9;
                control[6] = convert2integer(ABN[6], false) * 11;
                control[7] = convert2integer(ABN[7], false) * 13;
                control[8] = convert2integer(ABN[8], false) * 15;
                control[9] = convert2integer(ABN[9], false) * 17;
                control[10] = convert2integer(ABN[10], false) * 19;
                let total = 0;
                for (let i = 0; i < 11; i++)
                {
                    total += control[i];
                }

                if (total % 89 == 0) result = true;
                else result = false;
            }
            catch
            {
                result = false;
            }
        }
        else result = false;
        return result;
    }

History

  • 22nd August, 2020: Initial version
  • 24th August 2020: change variables name from spanish to english.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer DrUalcman
Philippines Philippines
A programmer with skill in c#, VB, MVC, .NET CORE, JAVASCRPT, HTML, ASP, CSS, windows forms, windows app.

I’m specialized in C# and Blazor WebAssembly, with solid experience in creating modern, fast, and scalable web applications. My work approach combines efficient development techniques with performance optimization, ensuring solutions that not only work but do so exceptionally well.

My work style is collaborative and results-oriented. I listen closely to my clients' needs and transform those ideas into technological solutions that exceed expectations. For me, each project is an opportunity to innovate, whether by improving an application's speed, optimizing databases, or integrating new technologies.

One of my most successful cases was optimizing a Blazor WebAssembly application, where I reduced the processing time from 26 to 9 seconds. This kind of result reflects my commitment to technical excellence and my ability to solve complex problems efficiently.

In addition to my work as a developer, I’m the creator of the blog https://aprende-a-programar.com, a platform dedicated to teaching programming in an accessible and practical way. This project not only demonstrates my passion for development but also my ability to communicate technical ideas clearly and simply, which makes collaboration with my clients easier.

My goal is to help the dev community with my experiencies, and sharing my code to everybody.

Comments and Discussions

 
GeneralA little refactor Pin
TheQult23-Aug-20 23:41
professionalTheQult23-Aug-20 23:41 
PraiseRe: A little refactor Pin
Sergi Ortiz Gomez24-Aug-20 0:07
professionalSergi Ortiz Gomez24-Aug-20 0:07 
GeneralRe: A little refactor Pin
kaos_12124-Aug-20 12:22
professionalkaos_12124-Aug-20 12:22 
GeneralHow not to code Pin
TheQult23-Aug-20 23:21
professionalTheQult23-Aug-20 23:21 
PraiseRe: How not to code Pin
Sergi Ortiz Gomez23-Aug-20 23:59
professionalSergi Ortiz Gomez23-Aug-20 23:59 
GeneralRe: How not to code Pin
Garth J Lancaster24-Aug-20 19:51
professionalGarth J Lancaster24-Aug-20 19:51 
This is not StackOverflow - It is a learning environment, so we can be a little kinder in our 'reviews', don't you think ?

1) ABN stands for Australian Business Number - like so many Governments, our Government is full of such numbers, ABN's, Australian Company Numbers (ACNs), Tax File Numbers (TFNs) .. you get the picture, I'm sure the Italian Government imposes the same on you.

BTW, "ABN, ACN, TFN" can all be validated using the same sort of algorithm, an array of 'weights' with which to multiply digits, and a modulus factor to check against.

2) Have you heard the expression "let he without sin cast the first stone" - you point out his mixture of english, spanish and spanglish, but have a spelling error in your 'rant', maybe English isn't your first language either, but hey, we are all friends here, and as some European colleagues once taught me, 'we all need to get along'

3) Your refactoring of his code is great, except for me you use 'var' where you don't need to in at least one instance ... but I am not going to start with "Jesus ...."

4) addition - as for the formatting, I don't think it's entirely his fault, it was correct, but seems to have screwed up when he edited the article - I'll see if I can replicate the issue and report it as a site bug, I have had it happen to me

giochiamo bene (hopefully that means 'lets play nice')


modified 25-Aug-20 2:22am.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.