Regex for ISBN Validation: The Pattern That Checks Format, Not the Checksum
Regex for ISBN validation has a hard limit that most tutorials skip: a regular expression can check whether an ISBN looks right, but it cannot check whether an ISBN is right, because the last digit is a checksum that only code can compute.
That limit is the whole story, so I am putting it first instead of burying it in a conclusion. The Regular Expressions Cookbook, which has the best treatment of this problem I have found, says it plainly: you cannot validate an ISBN using a regex alone, because the last digit is computed using a checksum algorithm. The regexes in that book validate format. The code examples that follow them validate the final digit. Any tutorial that promises full ISBN validation with a single pattern is selling you a format check with a better name.
Start with the two formats. ISBN-10 is the older format: 10 characters, nine digits plus a check digit that can be a digit or X. ISBN-13 is the current format: 13 digits, always starting with 978 or 979. Hyphens and spaces between the groups are presentation, not data. The same book can appear as 978-0-596-52068-7, as 978 0 596 52068 7, as 9780596520687, and with or without an ISBN or ISBN-13 prefix. A pattern that only accepts one of those spellings will reject real input from real catalogs, and a pattern that accepts all of them has to be careful not to accept nonsense in the process.
For most form validation, the honest approach is two steps. First, strip the hyphens, spaces, and any ISBN prefix from the input. Then test what is left against a digits only pattern. The simplest version that covers both formats is ^(?:\d{9}[\dXx]|\d{13})$, which accepts exactly 9 digits followed by a digit or X, or exactly 13 digits. That pattern is easy to read, easy to test, and it rejects the most common garbage: wrong lengths, letters in the middle, and extra characters. What it does not do is catch a transposed digit or a typo in the check digit, because a 13 digit string with one wrong digit still matches the shape perfectly.
What is the regex for ISBN validation with hyphens?
If you need to accept hyphens in the input itself, the cookbook pattern for ISBN-10 is ^(?:ISBN(?:-10)?:? ?)?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$)[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$, and the ISBN-13 version follows the same shape with a 97[89] prefix requirement. Those patterns are long because they are doing real work: the lookahead checks the total length with and without separators, and the body checks the group structure. They are worth copying from a reference rather than writing from memory, because a hand written version almost always gets one of the length checks wrong. In most codebases I would still prefer the strip first approach, because a 20 character pattern that you can read beats a 100 character pattern that you cannot.
Four mistakes account for most broken ISBN patterns I see. The first is allowing X anywhere, when X is only valid as the final check digit of an ISBN-10, and only in uppercase in the strict form. The second is forgetting the anchors, so an unanchored \d{10} pattern happily matches the first 10 digits of a 13 digit string and calls it valid. The third is accepting ISBN-13 numbers that do not start with 978 or 979, which are not valid ISBN-13 prefixes at all. The fourth is the big one: treating a format match as proof the ISBN exists. A format match only proves the string could be an ISBN. Only the checksum proves the digits are self consistent, and only a catalog lookup proves the book is real.
The middle of this problem is where most people stop too early, and it is worth slowing down for. Stripping separators feels like a cleanup step, but it is actually the step that decides whether your validation is strict or sloppy. If you strip hyphens and spaces, you must also decide what to do with input that has hyphens in the wrong places, like 978-059652068-7, where the groups do not match the real registration groups. The strip first approach accepts it as a format match. The cookbook pattern with its group length checks is stricter, but it still cannot verify that the groups correspond to a real publisher. There is no regex-only answer to that question. There is only a decision about how strict your form needs to be, and most forms do not need publisher group validation.
So here is the setup I would ship. Strip the ISBN prefix, hyphens, and spaces. Test the result against the digits only pattern. Then compute the checksum in code: for ISBN-13, multiply alternating digits by 1 and 3 and check the total against the final digit; for ISBN-10, use the weighted sum that produces X for a remainder of 10. If you skip the checksum step, say so in the code comment, because the next person to read the pattern will assume it validates more than it does. Build a regex pattern with plain English explanations, and test it against the real examples from this article, including the hyphenated, spaced, and bare forms, before you trust it on a live form. A pattern that passes your tests and fails on real catalog data is worse than no pattern at all, because it fails quietly.
Frequently asked questions
What is the regex for ISBN validation?
For digits only input after stripping hyphens and spaces, use ^(?:\d{9}[\dXx]|\d{13})$. It accepts a 10 character ISBN-10 or a 13 digit ISBN-13 by shape. It does not verify the checksum.
Can regex fully validate an ISBN?
No. The final digit of an ISBN is a checksum computed from the other digits, and regex cannot compute it. A regex can check format; code must verify the checksum.
Can X appear anywhere in an ISBN?
No. X is only valid as the final check digit of an ISBN-10, where it stands for the value 10. It should not appear in any other position, and it never appears in an ISBN-13.
What prefixes do ISBN-13 numbers start with?
ISBN-13 numbers start with 978 or 979. A 13 digit number starting with other digits is not a valid ISBN-13, even if it matches a generic 13 digit pattern.
Should I accept hyphens and spaces in ISBN input?
Yes, because catalogs and book covers print ISBNs with separators. Either accept them with a hyphen aware pattern from a reference like the Regular Expressions Cookbook, or strip them first and test the digits only form.
Related reading: Regex for Postal Code Validation · Regex for Credit Card Number Validation · Email Regex Validation Mistakes to Avoid · Regex URL Validation Mistakes to Avoid