Last active
December 20, 2015 11:09
-
-
Save Oblongmana/6121504 to your computer and use it in GitHub Desktop.
ACN and ABN Validation fro both Salesforce Apex and T-SQL. See http://www.oblongmana.com/articles/ABN-ACN-Validation-For-Apex-and-TSQL/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class ACNandABNValidation { | |
private enum ValidationType {ACN, ABN} | |
static final List<Long> ACN_WEIGHTS = new List<Long>{8,7,6,5,4,3,2,1}; | |
static final List<Long> ABN_WEIGHTS = new List<Long>{10,1,3,5,7,9,11,13,15,17,19}; | |
public static Boolean ValidateACN(String acnString) { | |
acnString = getNonBlankNumericStringWithoutWhitespace(acnString); | |
if(acnString == null) { | |
return false; | |
} | |
if(acnString.length() != 9) { | |
return false; | |
} | |
Integer strLength = acnString.length(); | |
Long givenCheck = Long.valueOf(acnString.substring(strLength - 1,strLength)); | |
Long acnWeightSum = calcWeightingSum(ValidationType.ACN, acnString); | |
Long modTenRemainder = Math.mod(acnWeightSum, 10); | |
Long calcCheck = (modTenRemainder == 0) ? 0 : (10 - modTenRemainder); | |
if(calcCheck != givenCheck) { | |
return false; | |
} | |
return true; | |
} | |
public static Boolean ValidateABN(String abnString) { | |
abnString = getNonBlankNumericStringWithoutWhitespace(abnString); | |
if(abnString == null) { | |
return false; | |
} | |
if(abnString.length() != 11) { | |
return false; | |
} | |
if(abnString.substring(0,1) == '0') { | |
return false; | |
} | |
String pos1Less1 = String.valueOf(Long.valueOf(abnString.substring(0,1))-1); | |
String modifiedABN = String.valueOf(pos1Less1 + abnString.substring(1)); | |
Long abnWeightingSum = calcWeightingSum(ValidationType.ABN, modifiedABN); | |
Long modEightyNineRemainder = Math.mod(abnWeightingSum, 89); | |
if(modEightyNineRemainder != 0) { | |
return false; | |
} | |
return true; | |
} | |
public static Boolean ValidateARBN(String arbnString) { | |
return ValidateACN(arbnString); | |
} | |
private static Long calcWeightingSum(ValidationType valType, String theNumString) { | |
List<Long> weightList = | |
(valType == ValidationType.ACN) ? ACN_WEIGHTS : ABN_WEIGHTS; | |
Long weightingSum = 0; | |
Integer startIndex = 0; | |
Integer endIndex = (valType == ValidationType.ACN ? 7 : 10); | |
for(Integer i = startIndex; i <= endIndex; i++) { | |
weightingSum += | |
( Long.valueOf(theNumString.substring(i,i+1) ) * weightList[i]); | |
} | |
return weightingSum; | |
} | |
private static String getNonBlankNumericStringWithoutWhitespace(String theString) { | |
if(String.isBlank(theString)) { | |
return null; | |
} | |
theString = theString.deleteWhitespace(); | |
if(!theString.isNumeric()) { | |
return null; | |
} | |
return theString; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
private class Test_ACNandABNValidation { | |
@IsTest | |
static void testACNValidation() { | |
System.assertEquals(true, ACNandABNValidation.ValidateACN('010749961')); //Valid # | |
System.assertEquals(false, ACNandABNValidation.ValidateACN('123456')); //# too short | |
System.assertEquals(false, ACNandABNValidation.ValidateACN('123456789')); //# right length, but invalid | |
System.assertEquals(false, ACNandABNValidation.ValidateACN('a2c4e6h8j')); //# contains non-numeric | |
} | |
@IsTest | |
static void testARBNValidation() { | |
System.assertEquals(true, ACNandABNValidation.ValidateARBN('010749961')); //Valid # | |
System.assertEquals(false, ACNandABNValidation.ValidateARBN('123456')); //# too short | |
System.assertEquals(false, ACNandABNValidation.ValidateARBN('123456789')); //# right length, but invalid | |
System.assertEquals(false, ACNandABNValidation.ValidateARBN('a2c4e6h8j')); //# contains non-numeric | |
} | |
@IsTest | |
static void testABNValidation() { | |
System.assertEquals(true, ACNandABNValidation.ValidateABN('53004085616')); //Valid # | |
System.assertEquals(false, ACNandABNValidation.ValidateABN('123456')); //# too short | |
System.assertEquals(false, ACNandABNValidation.ValidateABN('12345678911')); //# right length, but invalid | |
System.assertEquals(false, ACNandABNValidation.ValidateABN('a2c4e6h8j11')); //# contains non-numeric | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** ACN Validation ***/ | |
IF OBJECT_ID(N'dbo.VALIDATEACN', N'FN') IS NOT NULL | |
DROP FUNCTION dbo.VALIDATEACN ; | |
GO | |
CREATE FUNCTION VALIDATEACN (@acn VARCHAR(256)) | |
RETURNS BIT | |
WITH RETURNS NULL ON NULL INPUT | |
BEGIN | |
DECLARE @Outcome AS BIT ; | |
SET @acn = LTRIM(RTRIM(REPLACE(REPLACE(@acn,'-',''),' ',''))); | |
IF( | |
@acn <> '' | |
AND | |
@acn LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' | |
AND | |
LEN(@acn) = 9 | |
AND | |
RIGHT | |
( | |
10 - | |
( | |
( CAST( SUBSTRING( @acn, 1, 1 ) AS integer ) * 8 ) | |
+ ( CAST( SUBSTRING( @acn, 2, 1 ) AS integer ) * 7 ) | |
+ ( CAST( SUBSTRING( @acn, 3, 1 ) AS integer ) * 6 ) | |
+ ( CAST( SUBSTRING( @acn, 4, 1 ) AS integer ) * 5 ) | |
+ ( CAST( SUBSTRING( @acn, 5, 1 ) AS integer ) * 4 ) | |
+ ( CAST( SUBSTRING( @acn, 6, 1 ) AS integer ) * 3 ) | |
+ ( CAST( SUBSTRING( @acn, 7, 1 ) AS integer ) * 2 ) | |
+ ( CAST( SUBSTRING( @acn, 8, 1 ) AS integer ) * 1 ) | |
) | |
% 10 | |
, 1 | |
) | |
= | |
SUBSTRING(@acn,9,1) | |
) | |
Set @Outcome = 1; | |
ELSE | |
Set @Outcome = 0; | |
RETURN @Outcome | |
END; | |
GO | |
/*** ARBN Validation: Just an alias for VALIDATEACN ***/ | |
IF OBJECT_ID(N'dbo.VALIDATEARBN', N'FN') IS NOT NULL | |
DROP FUNCTION dbo.VALIDATEARBN ; | |
GO | |
CREATE FUNCTION VALIDATEARBN (@arbn VARCHAR(256)) | |
RETURNS BIT | |
WITH RETURNS NULL ON NULL INPUT | |
BEGIN | |
RETURN dbo.VALIDATEACN(@arbn); | |
END; | |
GO | |
/*** ABN Validation ***/ | |
IF OBJECT_ID(N'dbo.VALIDATEABN', N'FN') IS NOT NULL | |
DROP FUNCTION dbo.VALIDATEABN ; | |
GO | |
CREATE FUNCTION VALIDATEABN (@abn VARCHAR(256)) | |
RETURNS BIT | |
WITH RETURNS NULL ON NULL INPUT | |
BEGIN | |
DECLARE @Outcome AS BIT ; | |
SET @abn = LTRIM(RTRIM(REPLACE(REPLACE(@abn,'-',''),' ',''))); | |
IF( | |
@abn <> '' | |
AND | |
@abn LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' | |
AND | |
LEN(@abn) = 11 | |
AND | |
( | |
( ( CAST( SUBSTRING( @abn, 1, 1 ) AS integer ) - 1 ) * 10 ) | |
+ ( CAST( SUBSTRING( @abn, 2, 1 ) AS integer ) * 1 ) | |
+ ( CAST( SUBSTRING( @abn, 3, 1 ) AS integer ) * 3 ) | |
+ ( CAST( SUBSTRING( @abn, 4, 1 ) AS integer ) * 5 ) | |
+ ( CAST( SUBSTRING( @abn, 5, 1 ) AS integer ) * 7 ) | |
+ ( CAST( SUBSTRING( @abn, 6, 1 ) AS integer ) * 9 ) | |
+ ( CAST( SUBSTRING( @abn, 7, 1 ) AS integer ) * 11 ) | |
+ ( CAST( SUBSTRING( @abn, 8, 1 ) AS integer ) * 13 ) | |
+ ( CAST( SUBSTRING( @abn, 9, 1 ) AS integer ) * 15 ) | |
+ ( CAST( SUBSTRING( @abn, 10, 1 ) AS integer ) * 17 ) | |
+ ( CAST( SUBSTRING( @abn, 11, 1 ) AS integer ) * 19 ) | |
) % 89 | |
= 0 | |
) | |
Set @Outcome = 1; | |
ELSE | |
Set @Outcome = 0; | |
RETURN @Outcome | |
END; | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment