Yii 1.1: eccvalidator

Credit Card validator implementing Mod10 algorithm
12 followers

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

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

Total 6 comments

#16628 report it
Touzas at 2014/03/12 12:24pm
Corrections and updates

This is a corrections necesary to rules on PHP5. Modified: Line 125: creditCartExpiredYear >= $currentYear and Line 135: return !empty($creditCardHolder) && preg_match('/^[A-Za-z ]+$/',

Line 125: creditCartExpiredYear >= $currentYear and
Line 135: return !empty($creditCardHolder) && preg_match('/^[A-Za-z ]+$/', $creditCardHolder);
#8845 report it
Rajith R at 2012/07/03 04:13am
is this problem solved??

is this problem solved??(reported on forum by jharkins)

Forum Post

" On a side note, I'd like to report a minor bug. In the public method validateDate, it currently will not allow dates in the month of December "

#6316 report it
Antonio Ramirez at 2011/12/28 01:09pm
Hipercard

Not sure about that card, is Brazilian? Any links where I can get information about? Thanks!

#6307 report it
prchakal at 2011/12/27 09:49am
Hipercard

Hi,

Can you post validator for Hipercard?

#4506 report it
Ivica at 2011/07/14 10:17am
I am using it

Nice extension to validate Credit Cards. I don't know for other card types, but for Amex, Master Card, Visa and Discover, it is working perfect.

#4303 report it
sarveshb at 2011/06/23 06:37am
Validator

Thanks,Very easy to use

Leave a comment

Please to leave your comment.

Create extension