SIM Card/ICCID Validation

The Subscriber Identification Module (SIM) Card is an chip that stores your wireless devices’ International Mobile Subscriber Identity (IMSI). The SIM Card is in turn identified by an Integrated Circuit Card Identifier (ICCID) which is printed on the card.

I needed to identify valid ICCID values for a project I was working on, and more specifically SIM Cards that are valid on the Verizon LTE network in the United States. The ICCID values we are expecting will be 19 digits + a check digit for a total length of 20 numeric characters. The first two are hard coded to “89” for “telecommunications”, followed by a “1” for the United States country code, and then 3 digits identifying the Verizon Mobile Network Code (MNC) – see the previous link for valid values for other carriers – and then a final check digit using the Luhn algorithm.

In my code I first validate using a regular expression, since the regex should be decently efficient, and then calculate the Luhn check digit to make sure the number itself is correct.

<?php
class SimCardUtils {
	public static function isValidLuhn( $number ){
		// validate luhn checksum
		settype($number, 'string');
		$sumTable = array(
			array(0,1,2,3,4,5,6,7,8,9),
			array(0,2,4,6,8,1,3,5,7,9)
		);
		$sum = 0;
		$flip = 0;
		for ($i = strlen($number) - 1; $i >= 0; $i--) {
			$sum += $sumTable[$flip++ & 0x1][$number[$i]];
		}
		return $sum % 10 === 0;
	}

	public static function isValidSim( $sim_id ){
		// 89       = telecom
		// 1        = united states
		// [480-489] = verizon
		// {13}     = sim account
		// {1}      = luhn check digit
		$pattern = "/^(89)(1)(48[0-9])(\d{13})(\d)$/";

		// check to see if the pattern is valid followed by the Luhn checksum
		return ( false !== preg_match($pattern, $sim_id) && self::isValidLuhn( $sim_id ) );
	}
}
<?php
$iccids = array(
	'89148000000745809013', // valid
	'89148000000745809014', // invalid, wrong check digit
	'8914800000074580901',  // invalid, wrong length
);

foreach ( $iccids as $iccid ){
	echo $iccid . " is " . ( SimCardUtils::isValidSim( $iccid )? "valid" : "invalid" ) . ".\n";
}
89148000000745809013 is valid. 
89148000000745809014 is invalid. 
8914800000074580901 is invalid.

See this in action here http://codepad.viper-7.com/RcIxoH.

You may also like...

14 Responses

  1. Hi, thanks for this! Do you know the code to validate it directly in SQL (or TSQL)?

  2. honey says:

    hi.pls help me to recover my iccid.
    09493620743-smart/Philippines. because I got PUK in my phone

    • Justin Silver says:

      Your ICCID should be printed on your SIM card. This code is just meant to verify a value but can’t do any type of recovery.

  3. george says:

    I thought ICCIDS were 19 digits including the checksum not 20?

    but I may be wrong

    • Justin Silver says:

      You are somewhat correct – for GSM SIMs there are both 19 and 20 digit ICCIDs. Check out the Wikipedia page on it. The reality is that it’s sort of messy.

      With the GSM Phase 1 specification using 10 octets into which ICCID is stored as packed BCD, the data field has room for 20 digits with hexadecimal digit “F” being used as filler when necessary.
      In practice, this means that on GSM SIM cards there are 20-digit (19+1) and 19-digit (18+1) ICCIDs in use, depending upon the issuer. However, a single issuer always uses the same size for its ICCIDs.
      To confuse matters more, SIM factories seem to have varying ways of delivering electronic copies of SIM personalization datasets. Some datasets are without the ICCID checksum digit, others are with the digit.
      As required by E.118, The ITU regularly publishes a list of all internationally assigned IIN codes in its Operational Bulletins.

  4. Kelvin says:

    Please help validate my MDN and ICCID to enable me connect my phone to my PC

    • Justin Silver says:

      Hi Kelvin,

      MDN stands for Mobile Device Number, so it’s just your telephone number. The algorithm here can validate that your ICCID is in the correct format, but won’t be able to actually ensure that it is a working SIM ID. To do that, you’ll need to check with your cell phone company. All that said, you should only need this info to connect to the mobile network, not to your computer. That would be done via USB or Wifi most likely.

      Good luck!

  5. arjun says:

    Converting into Java Script

  6. jasmin says:

    Please help me retrieve my iccid since my sim is puk lock-smart-philippines, and my sim is alreadry cut into a micro sim and i’ve lost the part where the iccid is printed since my sim is very old

    • Justin Silver says:

      Hi Jasmin – this algorithm will just validate that a string of numbers is a valid SIM ICCID. If you have cut your SIM card and can’t read it then unfortunately there isn’t a way to figure it out by looking at it. The only thing I can suggest is to put it into a phone and then go to the Settings/Info (whatever that is on your phone) and it should show the SIM Card ID / ICCID somewhere. Good luck!

  7. Bright says:

    Am trying to open my hotspot on Verizon Samsung s5 but telling me my MDN or iccid is invalid

  8. Shailesh says:

    I purchased my mobile from USA and now I live in India and my galaxyS5 verizon mobile not working when start hotspot seen either MDN or ICCID is not valid how its solve and also when we call other sim error show on mobile screen

Leave a Reply

Your email address will not be published. Required fields are marked *