Intervention\Validation\Validator::isCreditcard PHP Method

isCreditcard() public static method

Checks if value is valid creditcard number.
public static isCreditcard ( mixed $value ) : boolean
$value mixed
return boolean
    public static function isCreditcard($value)
    {
        $length = strlen($value);
        if ($length < 13 || $length > 19) {
            return false;
        }
        $sum = 0;
        $weight = 2;
        for ($i = $length - 2; $i >= 0; $i--) {
            $digit = $weight * $value[$i];
            $sum += floor($digit / 10) + $digit % 10;
            $weight = $weight % 2 + 1;
        }
        $mod = (10 - $sum % 10) % 10;
        return $mod == $value[$length - 1];
    }

Usage Example

 public function testValidateCreditcard()
 {
     $cc = '4444111122223333';
     $no_cc = '9182819264532375';
     $this->assertTrue(Validator::isCreditcard($cc));
     $this->assertFalse(Validator::isCreditcard($no_cc));
 }