Software Development

Validate an ISBN value with VB.NET


The International Standard Book Number (ISBN) is a unique commercial book identifier barcode. If you work with VB.NET applications that deal with books, you're all-too-familiar with the task of confirming that a given ISBN value is valid.

Prior to 2007, ISBN consisted of 10-digit values; beginning this year, 13 digit ISBN numbers are used. The sample VB.NET code shows how to validate a 10 digit and a 13 digit ISBN value. (Note: The sample is not intended to be used in applications as-is -- it simply explains the basics of confirming an ISBN number is valid. Modify the code to best fit your needs.) I define variables to calculate both 10 digit and 13 digit ISBN values. Since the calculation techniques are slightly different for each type of ISBN, I use a separate set of variables for each type. The example hard codes two ISBN values -- one 10 digit and one 13 digit. Both values are valid ISBN numbers, and the code displays a message box for each ISBN value stating that it's valid. If you were to substitute the valid ISBN values with invalid numbers, the resulting message boxes would display corresponding messages.

The example defines variables to check any type of ISBN value to facilitate a better understanding of the code. However, you may want to create a function that will accept an ISBN value as a parameter, and then check whether the ISBN value passed has 10 or 13 digits, perform appropriate verification, and return the outcome of the verification.

Check out Wikipedia for more information on usage and verification of ISBNs.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a Project Manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

6 comments
Spinner of Websites
Spinner of Websites

Well, one could say that it does show rudimentary string conversion to numeric integers as it parses the string. As to "validation" of an ISBN number, it just validates that the string has numbers in it. To do a true validation, it would require more logic and much more code.

Spudplucker
Spudplucker

Ohmigod! Ever heard of For..Next Loops?

GeoSheehy
GeoSheehy

ISBN uses the character "X" to represent the value 10. The example uses the Convert.ToInt32 method which will fail if "X" is passed as the arg. -George Sheehy

alan williams
alan williams

Does not tak into account hyphens in the value or the X as a check Digit.

irina_medvinskaya
irina_medvinskaya

The code sample provided in the tip is not intended to be used in applications as is. It does however explain the basics of confirming an ISBN number is valid. You are welcome to modify it to best fit your needs.

Tillworks
Tillworks

How about this (C#) alternative: static bool IsISBNValid(string sInput) { // remove delimiters string sISBN = Regex.Replace(sInput, @"-|\.| ", ""); // validate number of digits if (sISBN.Length != 10 && sISBN.Length != 13) return false; // calculate the product of the digit multiplication operations int iProd = 0; for (int i = 0; i < (sISBN.Length - 1); i++) { if (sISBN.Length == 10) iProd += Convert.ToInt16(sISBN.Substring(i, 1)) * (10 - i); else iProd += Convert.ToInt16(sISBN.Substring(i, 1)) * (1 + (2 * (i % 2))); } // calculate the check digit string sCheckDigit; if (sISBN.Length == 10) { sCheckDigit = Convert.ToString((11 - (iProd % 11)) % 11); if (sCheckDigit == "10") sCheckDigit = "X"; } else sCheckDigit = Convert.ToString(10 - (iProd % 10)); // validate check digit return (sCheckDigit == sISBN.Substring(sISBN.Length - 1, 1)); }