Avatar of sparky74
sparky74
 asked on

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


Please can you help

ASP

Avatar of undefined
Last Comment
TimCottee

8/22/2022 - Mon
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
TimCottee

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

ASKER
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?

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TimCottee

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
sparky74

ASKER
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?


TimCottee

sparky74,

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

Tim
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sparky74

ASKER
Tim

I get the following error

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'Val'

/checkdigit.asp, line 13
ASKER CERTIFIED SOLUTION
TimCottee

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sparky74

ASKER
Thanks Tim

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

TimCottee

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck