Jump to content

rewrite function help


TheJoey

Recommended Posts

is_numeric returns true or false, so you end up passing true or false to luhn_check instead of the actual value.  You need to do something more like this:

 

if (is_numeric($cc) && luhn_check($cc)) 

 

Although...depending on what this luhn_check() function of yours does, it may not even be necessary... what does it do? Is it checking it to be a certain format already?

 

luhn check is a credit card functionality it works fine, but its not giving me errors for letters and such.

 

<?php 
/* 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.                      */

function luhn_check($number) {

  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  $number=preg_replace('/\D/', '', $number);

  // Set the string length and parity
  $number_length=strlen($number);
  $parity=$number_length % 2;

  // Loop through each digit and do the maths
  $total=0;
  for ($i=0; $i<$number_length; $i++) {
    $digit=$number[$i];
    // Multiply alternate digits by two
    if ($i % 2 == $parity) {
      $digit*=2;
      // If the sum is two digits, add them together (in effect)
      if ($digit > 9) {
        $digit-=9;
      }
    }
    // Total up the digits
    $total+=$digit;
  }

  // If the total mod 10 equals 0, the number is valid
  return ($total % 10 == 0) ? TRUE : FALSE;

}

?>

if (is_numeric($cc) && luhn_check($cc)) 
$error = false;
else
$error = true;	  

 

seems to be giving me error regardless

if (is_numeric($cc) && luhn_check($cc)) 
$error = false;
else
$error = true;	

 

well asuming you want error to be true when the luhn check is true and isnumeric is true, than the logic is wrong. right now, if the cc number is numeric, and the luhn check runs true, $error will be false. try

 

if (!is_numeric($cc) || !luhn_check($cc)) 
$error = false;
else
$error = true;	

if (!is_numeric($cc) && !luhn_check($cc)) 
$errorCC = true;
else
$errorCC = false;	  

 

this checks the algorithmn fine

just doesnt check for numeric

 

if (!is_numeric($cc) || !luhn_check($cc)) 
$errorCC = true;
else
$errorCC = false;	

 

this works well with numeric but doesnt use the luhn check well..

Lol i dont know wat to do.

if (!is_numeric($cc) && !luhn_check($cc)) 
$errorCC = true;
else
$errorCC = false;	  

 

this checks the algorithmn fine

just doesnt check for numeric

 

if (!is_numeric($cc) || !luhn_check($cc)) 
$errorCC = true;
else
$errorCC = false;	

 

this works well with numeric but doesnt use the luhn check well..

Lol i dont know wat to do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.