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
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
web development has never been so fun
www.2amigos.us
Validator
Thanks,Very easy to use
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.
Hipercard
Hi,
Can you post validator for Hipercard?
Hipercard
Not sure about that card, is Brazilian? Any links where I can get information about? Thanks!
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 "
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);
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.