regex Archives - Justin Silver https://www.justinsilver.com/tag/regex/ Technology, Travel, and Pictures Thu, 27 Aug 2015 18:10:28 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.1 https://www.justinsilver.com/wp-content/uploads/2013/06/cropped-apple-touch-icon-160x160.png regex Archives - Justin Silver https://www.justinsilver.com/tag/regex/ 32 32 SIM Card/ICCID Validation https://www.justinsilver.com/technology/sim-card-iccid-validation/?utm_source=rss&utm_medium=rss&utm_campaign=sim-card-iccid-validation https://www.justinsilver.com/technology/sim-card-iccid-validation/#comments Tue, 08 Apr 2014 21:45:37 +0000 http://justin.ag/?p=3390 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...

The post SIM Card/ICCID Validation appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

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.

The post SIM Card/ICCID Validation appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/sim-card-iccid-validation/feed/ 22