Luke White
Luke White

Reputation: 1

php credit card validation

I'm new to php and reading a couple of php books as well as going over some tutorials. I cant seem to get this php validation project to work write. Its seem not to get rid of any spaces or dashes. not sure what I'm doing wrong.

<html>
 <body>
  <h1>Validate Credit Card</h1><hr />

<form action="ValidateCreditCard.php" method="get" encytype="application/x-www-form -urlencoded">
 <p><input type="text" name="ccnumber" size="20" value="<?php if(!empty($_GET['ccnumber']))     echo $_GET['ccnumber'] ?>" /></p>   
  <p><input type="submit" value="Validate Credit Card" />
</form><hr />
   <?php
      if (!isset($_GET['ccnumber']))
    echo "<p>Enter your credit card number.</p>";
    else{
    $Payment = $_GET['ccnumber'];
    $ValidPayment = str_replace("_", "", $Payment);
    $ValidPayment = str_replace("_", "", $ValidPayment);
    if (!is_numberic($ValidPayment))
        echo "<p>You did not enter a valid credit card number!</p>";
        else
         echo "<p>Your credit card number is $ValidPayment.</p>";
    }   
?>

 </body>    
</html>

Upvotes: 0

Views: 1445

Answers (3)

Lars
Lars

Reputation: 5799

All credit card numbers implement verify digits to determine, if a credit card number is valid or if the user mistyped it. It usually incorporates specific numbers to be linked with an arithmetic operation and a modulo-operation to shrink the output space to a single digit. An example would to add the digits and perform a modulo(10)-Operation for each 3 digits. Supposedly, an entered number would be "2855-9649-2915", the last digit of each 4-digit-block would be a verify number:

2+8+5=15 => modulo10(15)=5 (correct)
9+6+4=19 => module10(19)=9 (correct)
2+9+1=12 => module10(12)=2 (not 5, so incorrect number was entered)

This page contains an explanation for credit cards and a php source code, that implements it.

EDIT: false numbers, corrected. Thx, Chris!

Upvotes: 0

Ben Swinburne
Ben Swinburne

Reputation: 26467

$ValidPayment = preg_replace( '/\D/', '', $_GET['ccnumber'] );

This will replace anything which is not numeric with nothing

Upvotes: 3

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

You are only attempting to remove underscores (_) and you're doing it twice.

Try this:

$Payment = preg_replace("/[^0-9]/","",$_GET['ccnumber']);
// do stuff with $Payment value, which is now only numbers

Upvotes: 4

Related Questions