Use Jakarta-ORO for ISBN validation. The Jakarta-ORO Java classes are a set of text-processing Java classes that provide Perl5 compatible regular expressions, AWK-like regular expressions, glob expressions, and utility classes for performing substitutions, splits, filtering filenames, etc.
Download Jakarta-ORO jar
import org.apache.oro.text.perl.Perl5Util; public class ISBNValidator { private static final String SEP = "(\\-|\\s)"; private static final String GROUP = "(\\d{1,5})"; private static final String PUBLISHER = "(\\d{1,7})"; private static final String TITLE = "(\\d{1,6})"; private static final String CHECK_ISBN10 = "([0-9X])"; private static final String CHECK_ISBN13 = "(\\d)"; private static final String PREFIX = "(978|979)"; private static final String ISBN10_PATTERN = "/^" + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + CHECK_ISBN10 + "$/"; private static final String ISBN13_PATTERN = "/^" + PREFIX + SEP + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + CHECK_ISBN13 + "$/"; private static final String EAN13_PATTERN = "/^" + PREFIX + "\\d{10}$/"; public ISBNValidator() { super(); } public boolean isValid(String isbn) { return isValidISBN10(isbn) || isValidISBN13(isbn); } public boolean isValidISBN10(String isbn) { if (isbn == null || isbn.length() < 10 || isbn.length() > 13) { return false; } if (isFormatted(isbn) && !isValidPatternISBN10(isbn)) { return false; } isbn = cleanISBN10(isbn); if (isbn.length() != 10) { return false; } return (sumISBN10(isbn) % 11) == 0; } public boolean isValidISBN13(String isbn) { if (isbn == null || isbn.length() < 13 || isbn.length() > 17) { return false; } if (isFormatted(isbn)) { if (!isValidPatternISBN13(isbn)) { return false; } isbn = toEAN13(isbn); } return isValidEAN13(isbn); } public boolean isValidEAN13(String ean13) { if (ean13 == null || ean13.length() != 13 || !isValidPatternEAN13(ean13)) { return false; } return (sumEAN13(ean13) % 10) == 0; } private int sumISBN10(String isbn) { int total = 0; for (int i = 0; i < 9; i++) { int weight = 10 - i; total += (weight * toInt(isbn.charAt(i))); } total += toInt(isbn.charAt(9)); // add check digit return total; } private int sumEAN13(String isbn) { int total = isbn.charAt(0); for (int i = 1; i < 13;) { total += (3 * isbn.charAt(i++)) + isbn.charAt(i++); } return total; } private String cleanISBN10(String isbn) { StringBuffer buf = new StringBuffer(10); for (int i = 0; i < isbn.length(); i++) { char digit = isbn.charAt(i); if (Character.isDigit(digit) || (digit == 'X')) { buf.append(digit); } } return buf.toString(); } private String toEAN13(String isbn) { StringBuffer buf = new StringBuffer(13); for (int i = 0; i < isbn.length(); i++) { char digit = isbn.charAt(i); if (Character.isDigit(digit)) { buf.append(digit); } } return buf.toString(); } private int toInt(char ch) { return (ch == 'X') ? 10 : Character.getNumericValue(ch); } private boolean isFormatted(String isbn) { return ((isbn.indexOf('-') != -1) || (isbn.indexOf(' ') != -1)); } private boolean isValidPatternISBN10(String isbn) { return new Perl5Util().match(ISBN10_PATTERN, isbn); } private boolean isValidPatternISBN13(String isbn) { return new Perl5Util().match(ISBN13_PATTERN, isbn); } private boolean isValidPatternEAN13(String isbn) { return new Perl5Util().match(EAN13_PATTERN, isbn); } }