Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if 10 digit string is valid Amazon ASIN

Tags:

php

amazon

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?

like image 941
RAD Moose Avatar asked Jan 23 '10 12:01

RAD Moose


People also ask

How many digits is a ASIN number?

Amazon Standard Identification Number (ASIN) is a ten-digit alphanumeric code that identifies products on Amazon. It's unique for each product and is assigned when you create a new product in Amazon's catalog.

Do all Amazon ASINs start with B?

They USUALLY start with B but not always, and they have a mix of letters and numbers, no spaces, so there isn't a "ASIN RegEx" method that would distinguish between a possible search term and ASIN.

Do all ASIN start with B0?

ASIN (Amazon Standard Identification Number) Finally, let's talk about the most crucial one. What is ASIN? Amazon Standard Identification Number is a ten alphanumeric digits sequence Amazon generates for every new listing. This one always starts with the magic “B0”.

How do I get an ASIN code for Amazon?

You can get the ASIN product ID of regular items by simply checking the web address of the Amazon listing, details of the listing, or using ASIN lookup tools. If you decide to list a unique product on Amazon, you must add a new ASIN to the listing. Usually, brands and manufacturers add a unique ASIN.


2 Answers

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 or includes what’s plausibly an ASIN:

/^\s*(B\d{2}[A-Z\d]{7}|\d{9}[X\d])\s*$/

or, without worrying about extra whitespace or capturing:

/^(B\d{2}[A-Z\d]{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:

  1. 10-digit ISBNs, which are 9 digits + a final character which may be a digit or "X".
  2. The letter B followed by two digits followed by seven ASCII-range alphanumeric characters (with alpha chars being uppercase).

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...)

like image 62
Semicolon Avatar answered Nov 05 '22 12:11

Semicolon


For PHP, there is a valid regular expression for ASINs here.

function isAsin($string){
    $ptn = "/B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(X|0-9])/";
    return preg_match($ptn, $string, $matches) === 1;
}
like image 41
Sebastian Viereck Avatar answered Nov 05 '22 12:11

Sebastian Viereck