# asp script Check digit calculation - 20 digit barcode

Hi

I am writing in asp and have a 19 digit code which i need to formulate to create a check digit for a barcode,

I have the formula, but have no idea on how to write this in asp.

my 19 digit barcode for the example is: 9673169126799000001?

the above number is stored in my database as 'barcode'

the check digit is calcualted based on the formula known as Modulus 10, factor 3:

NB. the positions are numbered from RIGHT to LEFT therefore the checkdigit is in position 1

Operation 1
Starting from position 2 of the barcode, add the digit values in even positions

Operation 2
Multiply the result of operation 1 by 3

Operation 3
starting from position 3 of the code, add the digit values in odd positions

Operation 4
add the results of operation 2 & 3

Operation 5
The check digit is the smallest number, which when added to the result of operation 4, gives a multiple of 10.

Example

Barcode number: 9673169 12 6799 000001?

Operation 1: 1+0+0+9+7+2+9+1+7+9  =45

Operation 2: 45 x 3                             =135

operation 3: 0+0+0+9+6+1+6+3+6      =31

Operation 4:135 + 31                          =166

Operation 5: 170 - 166                       =4

Check digit = 4

TimCottee

Hi sparky74,

Here is one quick solution:

Dim Barcode As String = "9673169126799000001"
Dim Evens As Int16 = 0
Dim Odds As Int16 = 0
Dim Flip As Boolean
Dim Pos As Int16
For Pos = Barcode.Length To 1 Step -1
If Flip Then
Odds += Val(Barcode.Substring(Pos - 1, 1))
Else
Evens += Val(Barcode.Substring(Pos - 1, 1))
End If
Flip = Not Flip
Next
Dim Check As Int16 = 10 - ((Evens * 3 + Odds) Mod 10)
MsgBox(Check)

Tim Cottee
Sorry that should be in asp not in .net so:

Dim Barcode
Barcode = "9673169126799000001"
Dim Evens
Dim Odds
Dim Flip
Dim Pos
Flip = False
For Pos = Len(Barcode) To 1 Step -1
If Flip Then
Odds = Odds + Val(Mid(Barcode, Pos, 1))
Else
Evens = Evens + Val(Mid(Barcode, Pos, 1))
End If
Flip = Not Flip
Next
Dim Check
Check = 10 - ((Evens * 3 + Odds) Mod 10)
Response.Write (Check)
sparky74

Hi Tim

Thanks, when i saved the bottom script as .aspx and that works fine, ideally i wanted to add this into my current .asp page but the top script crashes as follows

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/checkdigit.asp, line 2

Dim Barcode As String = "9673169126799000001"
------------^

do you know where its going wrong?

sparky74,

The top script was asp.net and the bottom one asp. The problem is that asp doesn't allow declaration of types for variables as well as not allowing assignation on the declaration line.

Tim

Tim

I this one works as .aspx

Dim Barcode As String = "9673169126799000001"
Dim Evens As Int16 = 0
Dim Odds As Int16 = 0
Dim Flip As Boolean
Dim Pos As Int16
For Pos = Barcode.Length To 1 Step -1
If Flip Then
Odds += Val(Barcode.Substring(Pos - 1, 1))
Else
Evens += Val(Barcode.Substring(Pos - 1, 1))
End If
Flip = Not Flip
Next
Dim Check As Int16 = 10 - ((Evens * 3 + Odds) Mod 10)
MsgBox(Check)

but this one doesn't work as   .asp

Dim Barcode
Barcode = "9673169126799000001"
Dim Evens
Dim Odds
Dim Flip
Dim Pos
Flip = False
For Pos = Len(Barcode) To 1 Step -1
If Flip Then
Odds = Odds + Val(Mid(Barcode, Pos, 1))
Else
Evens = Evens + Val(Mid(Barcode, Pos, 1))
End If
Flip = Not Flip
Next
Dim Check
Check = 10 - ((Evens * 3 + Odds) Mod 10)
Response.Write (Check)

is there any way to make the .asp script work?

sparky74,

What error do you get in asp with the second script? It worked fine for me as far as I could tell.

Tim

Tim

I get the following error

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'Val'

/checkdigit.asp, line 13
TimCottee

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.

Thanks Tim

I will use the .asp version, but interesting to see how the .net version works.

sparky74,

It is basically the same code, I did it first in asp.net as I wasn't paying attention to the fact you needed it in classic. Then just modified it to work in asp (not perfectly as it turns out).

Tim