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.
LinkedinTelecoms sim serial iccids
SHAMIM
01611258125
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
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!
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?
Hi Justin.
How would you validate IMSI numbers using Luhn algorithm ?
Thank you.
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
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
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!
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
Am trying to open my hotspot on Verizon Samsung s5 but telling me my MDN or iccid is invalid
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
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!
Converting into Java Script
Please help validate my MDN and ICCID to enable me connect my phone to my PC
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!
I thought ICCIDS were 19 digits including the checksum not 20?
but I may be wrong
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.
hi.pls help me to recover my iccid.
09493620743-smart/Philippines. because I got PUK in my phone
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.
Hi, thanks for this! Do you know the code to validate it directly in SQL (or TSQL)?
Hi Eduardo,
It’s been a while since I’ve used T-SQL, but I was able to find this guide on implementing the Luhn algorithm – http://www.mssqltips.com/sqlservertip/3319/implementing-luhns-algorithm-in-tsql-to-validate-credit-card-numbers/ – which you should be able to adjust for SIM ICCIDs. One thing to note is that although the info above *should* work, I have seen some instances of Verizon SIMs that passed but were invalid, or did not pass and were valid (at least according to a Verizon API I use to perform account maintenance), so YMMV.
Good luck!
WOW Justin, Thank You Very Much!!!