Strife_x7x
Strife_x7x

Reputation: 45

Validated a Credit Card Number

Need some help with this, I pretty have it finished but I can't get it to display or work correctly. Using str_replace, it's suppose to take the actual credit card number and remove the - from it. I have that part down, but when I create a nested if statement and use is_numeric to validate, it display "This is an invalid Card Number"; of course If I put it as is_numeric($CreditNumber)) it print out the Credit Card Number plus the echo as well.

Also, one thing I notice. I have this file uploaded to our class server and it displays the credit card number but it display it twice while when I use XAMPP it does not display it at all.

Any pointers? Here is the code

  <?php 

$CreditCard = array ("", "8910-1234-5678-6543", "OOOO-9123-4567-0123");

foreach ($CreditCard as $CreditNumber) {
    if(empty($CreditNumber)) {
        echo "Invalid Card Number <br/>";
    } else {
        $CreditNumber = str_replace("-", "", $CreditCard[1]);

        echo $CreditNumber;
        echo "<br/>";
        if (is_numeric()) {
            echo "This is an invalid Card Number";
        } else {
            echo $CreditNumber; //This shows the credit card number without the dashes
        }
    }
}

?>

EDIT:

I have it completed and it is showing the correct numbers at the end, but for some odd reason, it echos the 16-digit number twice

    <?php 

$CreditCard = array ("", "8910-1234-5678-6543", "OOOO-9123-4567-0123");

foreach ($CreditCard as $CreditNumber) {
    if(empty($CreditNumber)) {
        echo "Invalid Card Number <br/>";
    } else {
        $CreditNumber = str_replace("-", "", $CreditCard[1]);

        //echo $CreditNumber;
        //echo "<br/>";
        if (is_numeric($CreditCard)) {
            echo "This is an invalid Card Number";
        } else {
            echo $CreditNumber; //This shows the credit card number without the dashes
        }
    }
}

?>

Upvotes: 1

Views: 1412

Answers (3)

Shailendra Rajput
Shailendra Rajput

Reputation: 2879

We have to use Luhn algorithm for validating Credit card Number here is sample method for Validating Credit Card Number

// Function to validate a credit card number using the Luhn algorithm
function validateCreditCard($number)
{
    $number = str_replace(' ', '', $number);
    $sum = 0;
    $numDigits = strlen($number);
    $parity = $numDigits % 2;

    for ($i = 0; $i < $numDigits; $i++) {
        $digit = (int)$number[$i];

        if ($i % 2 == $parity) {
            $digit *= 2;

            if ($digit > 9) {
                $digit -= 9;
            }
        }

        $sum += $digit;
    }

    return ($sum % 10 == 0);
}

// Example usage
$creditCardNumber = "1234567812345670"; // Replace with your credit card number
if (validateCreditCard($creditCardNumber)) {
    echo "Valid credit card number.";
} else {
    echo "Invalid credit card number.";
}

Upvotes: 0

Syscall
Syscall

Reputation: 19780

In your loop, you override $CreditCard so, you cannot show the original format after. You could use a short function to make your tests in a scoped block:

function isCreditNumber($creditNumber) {
    if (empty($creditNumber)) {
        return false;
    }
    $num = str_replace("-", "", $creditNumber);
    if (! is_numeric($num)) {
        return false;
    }
    return true;
}

$CreditCard = ["", "8910-1234-5678-6543", "OOOO-9123-4567-0123"];

foreach ($CreditCard as $CreditNumber) {
    if (! isCreditNumber($CreditNumber)) {
        echo "Invalid Card Number <br/>";
    }
    else {
        echo $CreditNumber . " is valid<br>";
    }
}

Output:

Invalid Card Number
8910-1234-5678-6543 is valid
Invalid Card Number

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

Regular expressions can be useful here:

$CreditCard = array("", "8910-1234-5678-6543", "OOOO-9123-4567-0123");

foreach ($CreditCard as $CreditNumber) {
    if (preg_match("/^\d{4}-\d{4}-\d{4}-\d{4}$/", $CreditNumber)) {
        echo "VALID:   " . $CreditNumber . "\n";
    }
    else {
        echo "INVALID: " . $CreditNumber . "\n";
    }
}

This prints:

INVALID: 
VALID:   8910-1234-5678-6543
INVALID: OOOO-9123-4567-0123

Note that if you want regex patterns to validate specific vendor cards (e.g. Amex, Mastercard, Visa, etc.), then use a reference, such as this one.

Upvotes: 0

Related Questions