Skip to content

Commit 83d6ffd

Browse files
fix(isVAT): improved ABN (AU VAT) validation (validatorjs#2343)
1 parent 43a0f09 commit 83d6ffd

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/lib/isVAT.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import assertString from './util/assertString';
22
import * as algorithms from './util/algorithms';
33

4+
const AU = (str) => {
5+
const match = str.match(/^(AU)?(\d{11})$/);
6+
if (!match) {
7+
return false;
8+
}
9+
// @see {@link https://abr.business.gov.au/Help/AbnFormat}
10+
const weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
11+
str = str.replace(/^AU/, '');
12+
const ABN = (parseInt(str.slice(0, 1), 10) - 1).toString() + str.slice(1);
13+
let total = 0;
14+
for (let i = 0; i < 11; i++) {
15+
total += weights[i] * ABN.charAt(i);
16+
}
17+
return (total !== 0 && total % 89 === 0);
18+
};
19+
420
const CH = (str) => {
521
// @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0}
622
const hasValidCheckNumber = (digits) => {
@@ -68,7 +84,7 @@ export const vatMatchers = {
6884
*/
6985
AL: str => /^(AL)?\w{9}[A-Z]$/.test(str),
7086
MK: str => /^(MK)?\d{13}$/.test(str),
71-
AU: str => /^(AU)?\d{11}$/.test(str),
87+
AU,
7288
BY: str => /^(УНП )?\d{9}$/.test(str),
7389
CA: str => /^(CA)?\d{9}$/.test(str),
7490
IS: str => /^(IS)?\d{5,6}$/.test(str),

test/validators.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14105,10 +14105,24 @@ describe('Validators', () => {
1410514105
validator: 'isVAT',
1410614106
args: ['AU'],
1410714107
valid: [
14108+
'AU53004085616',
14109+
'53004085616',
14110+
'AU65613309809',
14111+
'65613309809',
14112+
'AU34118972998',
14113+
'34118972998',
14114+
],
14115+
invalid: [
14116+
'AU65613309808',
14117+
'65613309808',
14118+
'AU55613309809',
14119+
'55613309809',
14120+
'AU65613319809',
14121+
'65613319809',
14122+
'AU34117972998',
14123+
'34117972998',
1410814124
'AU12345678901',
1410914125
'12345678901',
14110-
],
14111-
invalid: [
1411214126
'AU 12345678901',
1411314127
'1234567890',
1411414128
],

0 commit comments

Comments
 (0)