Modulus11

ronanjordan

Member
Joined
Apr 10, 2006
Messages
9
Programming Experience
Beginner
Hi All,

I need to populate ISBN No on btnSubmit_Click event using Modulus11 check. The input box is a text box " txtISBN" . Any idea how I can implement this?
Any help appreciated:confused:
 
I'll take a stab at this

I assume that when you say "populate" that you are running an SQL query on a database? If so, this shouldn't be too difficult. Assuming that you know how to construct queries in your table adapter, all you need to do is parse the input textbox, run a "Modulus 11" algorithm on the number, and compare that result to the "Modulus 11" result from the value extracted from your DB. Here is a web site I found that explains the Modulus 11 calculation.

http://www.school-resources.co.uk/modulus_11_check_digit_calculati.htm

If I am totally wrong about your question, please elaborate.

Scott D.
 
Now that I actually read through the whole page that I posted, I think I see what your question is. You want to check the validity of the input number before querying the DB? If so, you'll have to write the algorithm and use the appropriate return value to determine if the number is valid before performing the query. Is this correct?

If you need help writting the algorithm, I'd be happy to give it a shot!

Scott D.
 
Hi Scott,

Thanks for your reply.
Yes, I need to populate the input before querying the DB. I do understand the logic but I don't know how actually will work as I am novice in VB. I will appreciate if you can help and create something.

RonanJ
 
The text box will contain string data type ex. 0123454906 (10 digits)
I have tryed to do the folowing
VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] Modulus11([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DigitType [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] txtISBN [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tmpNumber, NewNumber, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, Asum, Bsum, Csum, Dsum, Esum[/SIZE]
[SIZE=2]tmpNumber = Trim(txtISBN) [/SIZE][SIZE=2][COLOR=#008000]'Calculating the eaiting factor[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]A1 = Mid(tmpNumber, 1, 1) * 10[/SIZE]
[SIZE=2]A2 = Mid(tmpNumber, 2, 2) * 9[/SIZE]
[SIZE=2]A3 = Mid(tmpNumber, 3, 3) * 8[/SIZE]
[SIZE=2]A4 = Mid(tmpNumber, 4, 4) * 7[/SIZE]
[SIZE=2]A5 = Mid(tmpNumber, 5, 5) * 6[/SIZE]
[SIZE=2]A6 = Mid(tmpNumber, 6, 6) * 5[/SIZE]
[SIZE=2]A7 = Mid(tmpNumber, 7, 7) * 4[/SIZE]
[SIZE=2]A8 = Mid(tmpNumber, 8, 8) * 3[/SIZE]
[SIZE=2]A9 = Mid(tmpNumber, 9, 9) * 2[/SIZE]
[SIZE=2]A10 = Mid(tmpNumber, 10, 10) * 1[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] InvalidCastException[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]Asum = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10[/SIZE]
[SIZE=2]Bsum = Asum [/SIZE][SIZE=2][COLOR=#0000ff]Mod[/COLOR][/SIZE][SIZE=2] 11 [/SIZE][SIZE=2][COLOR=#008000]'Reminder[/COLOR][/SIZE]
[SIZE=2]Csum = 11 - Bsum[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Csum = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]MsgBox("valid ISBN")[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]MsgBox("Invalid")[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
The problem I had is geting string data containing 10 haracters and cast it in Array(9) (10 blocks) as each number will represent each block in the array.
The code above is only a try and I wrote it after reading the same website
 
Last edited by a moderator:
Here is a static class that will 1) calculate check values and 2) check a given ISBN for validity.

VB.NET:
Public Class ISBN

    Public Shared Function IsValid(ByVal number As String) As Boolean
        Return number.Length = 10 AndAlso number.Chars(number.Length - 1).ToString() = ISBN.GetCheckNumber(number)

    End Function

    Public Shared Function GetCheckNumber(ByVal number As String) As String
        Dim weights() As Integer = {10, 9, 8, 7, 6, 5, 4, 3, 2}
        Dim numbers() As Integer = {Integer.Parse(number.Chars(0)), _
        Integer.Parse(number.Chars(1)), Integer.Parse(number.Chars(2)), _
        Integer.Parse(number.Chars(3)), Integer.Parse(number.Chars(4)), _
        Integer.Parse(number.Chars(5)), Integer.Parse(number.Chars(6)), _
        Integer.Parse(number.Chars(7)), Integer.Parse(number.Chars(8))}

        ' multiply all of the weighting factors
        Dim i As Integer
        Dim i_result(8) As Integer
        For i = 0 To 8
            i_result(i) = weights(i) * numbers(i)
        Next i

        ' get the sum
        Dim sum As Integer = 0
        For i = 0 To 8
            sum += i_result(i)
        Next i

        ' get the remainder
        Dim check As Integer = sum Mod 11

        If check = 10 Then
            Return "X"
        Else
            Return check.ToString()
        End If

    End Function
End Class

Maybe this will help.
 
Back
Top