0

I wanted to start developed my own payment system and i wanted to learn as much as i want on this so first i started with credit card validation algorithm mod 10

<?php
  $array = $_POST['array'];  //array holding numbers
  $array = array_reverse($array);          // reversing the array
  echo "<br> Credit card Before :- ";
  for($i=0;$i<sizeof($array);$i++)
    echo $array[$i];
  echo "<br>";
  echo "<br> Credit card after applaying mod 10 Algorthim :- ";
  //start converting the number
  for($i=0;$i<sizeof($array);$i++){
    if($i%2==1){
      $array[$i] = $array[$i]+$array[$i];
        if($array[$i]> 9 ){
          $str = "$array[$i]";
          $num = $str[0]+$str[1];
          $array[$i] = $num;
        }
      }
      //suming all the resulted number
      $total +=$array[$i];
      if($i%2==1)
        echo "<b>".$array[$i]."</b>";
      else
        echo $array[$i];
    }
    echo "<br>";
      if($total % 10 ==0){
        echo "This creidet card is valid";
      }else{
        echo "This creidet card is not valid";
      }
?>
tomwhipple
  • 2,850
  • 27
  • 28
yasir ALQAISI
  • 59
  • 1
  • 8

1 Answers1

0

I recommend you to use Regular expression... You can check my repository I have a code snippet to validate Visa, Master card, Discovery, american express and the Security code

https://github.com/leojavier/RegexCollection


Remember... just because you limit the amount of digits doesn't mean it will be right.

for instance:
- All Visa card numbers start with a 4. New cards have 16 digits.
- All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. and so on...

The index.html is a sample, to show you how you can use it! Feel free to write me if you have any questions :)

Leo Javier
  • 1,383
  • 12
  • 19