eccvalidator Credit Card validator implementing Mod10 algorithm

  1. Requirements
  2. Usage
  3. Note
  4. Resources
  5. Change Log
  6. 1.0.2
  7. 1.0.1
  8. 1.0

This is a credit card validator class for your projects. It validates credit card numbers prefix and uses Mod 10 algorithm to detect single-digit errors.

Currently supports:

  • Maestro
  • Solo
  • Electron
  • American Express
  • Mastercard
  • Discover
  • JCB
  • Voyager
  • Diners Club
  • Switch
  • Laser
  • VISA
I have created a GitHub repository for those willing to contribute on any of the extensions I created. Please, check the link at the bottom of this wiki.

Requirements

Developed using 1.1.6

Usage

Unzip the contents of the zip file onto your protected/extensions folder, then you can use the validator class in the following ways:

As a attribute validator
Due to the nature of validateAttribute function, only CC numbers are validated through this function. Please note that if you do not set its format, then the validator will check against all supported Credit Card types.

// On your model's rules function
public function rules()
{
   // importing the validator to make use of its 
   // constants. You can also preload the class on your main.php file
   Yii::import('ext.validators.ECCValidator');

   return array(
     array('cc','ext.validators.ECCValidator',
        'format'=>ECCValidator::MASTERCARD),
        // You can also check multiple type of cards
        // 'format'=>array(ECCValidator::MASTERCARD, ECCValidator::VISA)),
	);
   
}

As standalone class
Multiple CC checking (without using ECCValidator::ALL)

Yii::import('ext.validators.ECCValidator');

$cc = new ECCValidator();
$cc->format = array(ECCValidator::MASTERCARD, ECCValidator::VISA);
$card = '5500 0000 0000 0004';
echo 'MASTERCARD VALIDATION [5500 0000 0000 0004]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';

$card = '4539 6422 3195 9702';
echo 'VISA VALIDATION 16 [4539 6422 3195 9702]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
Yii::import('ext.validators.ECCValidator');
	
$cc = new ECCValidator();
$cc->format = ECCValidator::MASTERCARD;
$card = '5500 0000 0000 0004';
echo 'MASTERCARD VALIDATION [5500 0000 0000 0004]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::AMERICAN_EXPRESS;
$card = '3782 8224 6310 005';
echo 'American Express VALIDATION [3782 8224 6310 005]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::DINERS_CLUB;
$card = '3852 0000 0232 37';
echo 'DINERS CLUB VALIDATION [3852 0000 0232 37]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::JCB;
$card = '3088 5749 0496 7422';
echo 'JCB VALIDATION 16 [3088 5749 0496 7422]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
$card = '1800 9030 7342 009';
echo 'JCB VALIDATION 15 [1800 9030 7342 009]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::VISA;
$card = '4539 6422 3195 9702';
echo 'VISA VALIDATION 16 [4539 6422 3195 9702]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$card = '4929 857 283 556';
echo 'VISA VALIDATION 13 [4929 857 283 556]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
	
$cc->format = ECCValidator::DISCOVER;
$card = '6011 3379 3190 5012';
echo 'DISCOVER VALIDATION [6011 3379 3190 5012]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::VOYAGER;
$card = '8699 4096 4429 408';
echo 'VOYAGER VALIDATION [8699 4096 4429 408]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::ELECTRON;
$card = '4917300800000000';
echo 'VISA ELECTRON VALIDATION [4917300800000000]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
$cc->format = ECCValidator::LASER;
$card = '6304 9000 1774 0292 441';
echo 'LASER VALIDATION 19 [6304 9000 1774 0292 441]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
		
	
$cc->format = ECCValidator::LASER;
$card = '6304 9506 0000 0000 00';
echo 'LASER VALIDATION 18 [6304 9506 0000 0000 00]: '. ($cc->validateNumber($card)?'ok':'nok').'<br/>';
	
echo 'TESTING NAME ANTONIO RAMIREZ: '. ($cc->validateName('ANTONIO RAMIREZ')?'ok':'nok').'<br/>';
echo 'TESTING NAME ANTONIO RAMIREZ 56: '. ($cc->validateName('ANTONIO RAMIREZ 56')?'ok':'nok').'<br/>';
	
echo 'TESTING DATE 08/2012: '. ($cc->validateDate('08', 2012)?'ok':'nok').'<br/>';
echo 'TESTING DATE 08/2062: '. ($cc->validateDate(8, 2062)?'ok':'nok').'<br/>';

echo 'VALIDATING ALL [LASER]: '.($cc->validateAll('ANTONIO RAMIREZ', '6304 9506 0000 0000 00', '08', 2012)?'ok':'nok').'<br/>';

Note

Please make use of the forum post to report errors, requests, and suggestions. Let comments on this extension for coding hints.

Resources

Change Log

1.0.2

  • 22-08-2011 Fixed date validation bug (Thanks jharkins)

    1.0.1

  • 13-02-2011 Fixed regular expression bug for ECCValidator::ALL check
  • 13-02-2011 Implemented the option for multiple card checking (Thanks mdomba)

    1.0

  • Initial public release

2amigOS!
web development has never been so fun
www.2amigos.us

5 0
11 followers
1 510 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: Antonio Ramirez
Created on: Feb 13, 2011
Last updated: 11 years ago

Downloads

show all

Related Extensions