I have a 10 digit string being passed to me, and I want to verify that it is a valid ASIN before doing more processing and/or redirection.
I know that a non ISBN ASIN will always be non-numeric and 10 characters in length
I just want to be able to tell if the item being passed is a valid ASIN or is it just a search string after I have already eliminated that it could be a ISBN.
For example "SOUNDBOARD" is a search term while "B000J5XS3C" is an ASIN and "1412775884" is an ISBN.
Is there a lightweight way to check ASIN?
Did you know that Amazon offers an API, including an Amazon Associates Web Service, that allows you to interactive programatically with Amazon. I suspect that will solve your problem (in some fashion). Check out the Amazon Web Services home page for more info.
Update, 2017
@Leonid commented that he’s found the ASIN BT00LLINKI
.
Although ASIN’s don’t seem to be strictly incremental, the oldest non-ISBN ASINs do tend to have more zeros than newer ASINs. Perhaps it was inevitable that we’d start seeing ASINs with no zero padding (and then what, I wonder...). So we’re now looking for "B" followed by nine alphanumeric characters (or an ISBN) — unfortunately, the "loss" of that zero makes it a lot easier to get a false positive.
/^B[\dA-Z]{9}|\d{9}(X|\d)$/
Original answer
In Javascript, I use the following regexp to determine whether a string is plausibly an ASIN:
/^\s*(B\d{3}\w{6}|\d{9}(?:X|\d))\s*$/
or, without worrying about extra whitespace or capturing:
/^B\d{2}\w{7}|\d{9}(X|\d)$/
As others have mentioned, Amazon hasn't really revealed the spec. In practice I've only seen two possible formats for ASINs, though:
- 10-digit ISBNs, which are 9 digits + a final character which may be a digit or "X"
- The letter B followed by two or three digits followed by six or seven alphanumeric characters
If anyone has encountered an ASIN that doesn't fit that pattern, chime in. It may actually be possible to get more restrictive than this, but I'm not certain. Non-ISBN ASINs might only use a subset of alphabetic characters, but even if so, they do use most of them. Some seem to appear more frequently than others, at least (K, Z, Q, W...)
For PHP there is a valid regular expression for Asins here: http://www.sebastianviereck.de/en/php-ueberpruefen-ob-ein-string-eine-valide-asin-ist/ (English version)
maybe you could check on the amazon site whether the ASIN exists.
http://www.amazon.com/dp/YOUR10DIGITASIN
this URL return a http-statuscode=200 when the product exists and a 404 if that was not a valid ASIN.
"this URL return a http-statuscode=200 when the product exists and a 404 if that was not a valid ASIN."
this will NOT work, since according to the docs ASINs are region specific (check it for yourself if you don't believe). you can only verify an asin from - let's say - amazon.co.uk at amazon.co.uk, so you also have to know whre the ASIN comes from.
however, in your case you're better off having three input fields - one for each search. or (much better) one field an three radio buttons. alternatively, you could check the string angainst a dictionary... but guessing is alsways VERY bad engineering.
moreover, there is no lightweight way to check an asin for validity
After trying couple of solutions (including the top voted answer) they did not work well in PHP. (ex. 8619203011 is shown as ASIN)
Here is the solution that works very well:
function isAsin($string){
$ptn = "/^(?i)(B0|BT)[0-9A-Z]{8}$/";
if (preg_match($ptn, $string, $matches)) {
return true;
}
}
$testAsins = array('k023l5bix8', 'bb03l5bix8', 'b143l5bix8', 'bt00plinki', ' ', '');
foreach ($testAsins as $testAsin) {
if(isAsin($testAsin)){
echo $testAsin." is ASIN"."<br>";
} else {
echo $testAsin." is NOT ASIN"."<br>";
}
}
Explanation:
/^(?i)(B0|BT)[0-9A-Z]{8}$/
/^ = Beginning
(?i) = Case in-sensitive
(B0|BT)= Starting with B0 or BT
[0-9A-Z]= any numbers or letters
{8} = 8 numbers or letters allowed (on top of +2 from B0 or BT)