<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Plan Zero Blog &#187; Code Snippets</title>
	<atom:link href="http://blog.planzero.org/category/code/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.planzero.org</link>
	<description>Mind dump</description>
	<lastBuildDate>Sat, 05 Sep 2009 17:29:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript Luhn modulus implementation</title>
		<link>http://blog.planzero.org/2009/08/javascript-luhn-modulus-implementation/</link>
		<comments>http://blog.planzero.org/2009/08/javascript-luhn-modulus-implementation/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 21:43:01 +0000</pubDate>
		<dc:creator>Plan Zero</dc:creator>
				<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://blog.planzero.org/?p=267</guid>
		<description><![CDATA[<p>The following luhn_check() function is a small JavaScript function which checks whether a number is a valid based on the Luhn algoritm. One common example of use is with credit and debit card numbers. This function may be used to check for valid credit card numbers, however additional checks may be desired, such as card prefix and length checks.</p>
<p>There&#8217;s a&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>The following luhn_check() function is a small JavaScript function which checks whether a number is a valid based on the Luhn algoritm. One common example of use is with credit and debit card numbers. This function may be used to check for valid credit card numbers, however additional checks may be desired, such as card prefix and length checks.</p>
<p>There&#8217;s a <a href="http://planzero.org/bits/example_luhn_check">working example</a> of this function to give you more of an idea of how it works!
<pre></pre>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Luhn algorithm number checker - (c) 2005-2009 - planzero.org            *
 * This code has been released into the public domain, however please      *
 * give credit to the original author where possible.                      */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> luhn_check<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">//&lt;![CDATA[</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Strip any non-digits (useful for credit card numbers with spaces and hyphens)</span>
  <span style="color: #003366; font-weight: bold;">var</span> number<span style="color: #339933;">=</span>number.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\D/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Set the string length and parity</span>
  <span style="color: #003366; font-weight: bold;">var</span> number_length<span style="color: #339933;">=</span>number.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> parity<span style="color: #339933;">=</span>number_length <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Loop through each digit and do the maths</span>
  <span style="color: #003366; font-weight: bold;">var</span> total<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> number_length<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> digit<span style="color: #339933;">=</span>number.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Multiply alternate digits by two</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">==</span> parity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      digit<span style="color: #339933;">=</span>digit <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
      <span style="color: #006600; font-style: italic;">// If the sum is two digits, add them together (in effect)</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>digit <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        digit<span style="color: #339933;">=</span>digit <span style="color: #339933;">-</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// Total up the digits</span>
    total <span style="color: #339933;">=</span> total <span style="color: #339933;">+</span> parseInt<span style="color: #009900;">&#40;</span>digit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// If the total mod 10 equals 0, the number is valid</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>total <span style="color: #339933;">%</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//]]&gt;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.planzero.org/2009/08/javascript-luhn-modulus-implementation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Luhn modulus implementation</title>
		<link>http://blog.planzero.org/2009/08/luhn-modulus-implementation-php/</link>
		<comments>http://blog.planzero.org/2009/08/luhn-modulus-implementation-php/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 21:28:51 +0000</pubDate>
		<dc:creator>Plan Zero</dc:creator>
				<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://blog.planzero.org/?p=250</guid>
		<description><![CDATA[<p>The following <code>luhn_check()</code> function is a small PHP function which checks whether a number is a valid based on the <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Luhn algorithm</a>. One common example of use is with credit and debit card numbers. This function may be used to check for valid credit card numbers, however additional checks may be desired, such as card prefix and length checks.
<pre></pre>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&#60;?php</span>
<span style="color: #666666; font-style: italic;">/* Luhn&#8230;</span></pre></p>]]></description>
			<content:encoded><![CDATA[<p>The following <code>luhn_check()</code> function is a small PHP function which checks whether a number is a valid based on the <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Luhn algorithm</a>. One common example of use is with credit and debit card numbers. This function may be used to check for valid credit card numbers, however additional checks may be desired, such as card prefix and length checks.
<pre></pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* Luhn algorithm number checker - (c) 2005-2008 - planzero.org            *
 * This code has been released into the public domain, however please      *
 * give credit to the original author where possible.                      */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> luhn_check<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Strip any non-digits (useful for credit card numbers with spaces and hyphens)</span>
  <span style="color: #000088;">$number</span><span style="color: #339933;">=</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\D/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Set the string length and parity</span>
  <span style="color: #000088;">$number_length</span><span style="color: #339933;">=</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$parity</span><span style="color: #339933;">=</span><span style="color: #000088;">$number_length</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Loop through each digit and do the maths</span>
  <span style="color: #000088;">$total</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$number_length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$digit</span><span style="color: #339933;">=</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Multiply alternate digits by two</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$parity</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$digit</span><span style="color: #339933;">*=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// If the sum is two digits, add them together (in effect)</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$digit</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$digit</span><span style="color: #339933;">-=</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// Total up the digits</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">+=</span><span style="color: #000088;">$digit</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// If the total mod 10 equals 0, the number is valid</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$total</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.planzero.org/2009/08/luhn-modulus-implementation-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
