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

22 Responses

  1. SHAMIM says:

    LinkedinTelecoms sim serial iccids
    SHAMIM
    01611258125

  2. Muhammad Shahroz says:

    Hi Justin,

    I am working on WiFi project in which i have to fetch the ICCID number of any Android phone when it is in contact with as AP and AP redirect it to the portal server and portal server pushed a page, when user click on it the server runs a script at back end and fetch its ICCID number and stored in Database as well and user connected to the internet.

    Can you help in this, have you a code of this:

    thanks

    Regards,
    Shahroz

    • Justin Silver says:

      Hi Muhammad,

      As far as I know this isn’t possible – the Wifi radio is separate from the mobile radio, and I really doubt that the SIM card ICCID would be shared over it. A Stingray can do it, but you are basically just intercepting the cell phone traffic which isn’t exactly legal to do unless you are law enforcement.

      Your best bet for an ID over Wifi would be the MAC address I think.

      Good luck!

  3. David Cole says:

    I found the 4G LTE Verizon SIM card in my junk drawer and when I went to activate it in a phone that I had sitting around they said that it was reported lost or stolen. The SIM card not the device! Is there any way to fix this SIM card to be usable again?

  4. Gunnlaugur says:

    Hi Justin.
    How would you validate IMSI numbers using Luhn algorithm ?
    Thank you.

    • Justin Silver says:

      I don’t think that the IMSI is generated with a Luhn checksum so I don’t think you can – but I could be mistaken. This is what I found about the format: http://www.imsiadmin.com/imsi_faq.cfm

      The IMSI is a fixed 15-digit length. It consists of a 3-digit Mobile Country Code (MCC), a 3-digit Mobile Network Code (MNC), and a 9-digit Mobile Station Identification Number (MSIN). Telcordia Technologies is responsible for the assignment and administration of the first six digits (the MCC + MNC) to network operators, and the network operator to which these digits are assigned is responsible for the assignment and administration of the last 9 digits (the MSIN).

  5. Jude says:

    Galaxy s5 says either mdn or iccid not valid while powering the hotspot. How can I resolve it. It’s a Verizon phone but my brother sent it to me in nigeria

    • Justin Silver says:

      Hi Jude, this code is just meant to validate the format of an ICCID but that does not ensure that it is valid for your network. Good luck!

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

  7. Bright says:

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

  8. 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!

  9. arjun says:

    Converting into Java Script

  10. 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!

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

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

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

Leave a Reply

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