Credit card verification procedure

Source: Internet
Author: User
Tags ereg
& Lt ;? Php & nbsp; Creditcardvalidationroutine> May15, 2000
//////////////////////////////////////// ////////////
////
// Credit card validation routine //
// May 15,200 0 //
// By ariso //
// ValidateCardCode ($ number [, $ cardtype]) //
//////////////////////////////////////// ////////////


Function validateCardCode ($ cardnumber, $ cardtype = 'unknown ')
{
// Clean up input

$ Cardtype = strtolower ($ cardtype );
$ Cardnumber = ereg_replace ('[-[: space:]', '', $ cardnumber );

// Do type specific checks

If ($ cardtype = 'unknown '){
// Skip type specific checks
}
Elseif ($ cardtype = 'MasterCard '){
If (strlen ($ cardnumber )! = 16 |! Ereg ('5 [1-5] ', $ cardnumber) return 0;
}
Elseif ($ cardtype = 'visa '){
If (strlen ($ cardnumber )! = 13 & strlen ($ cardnumber )! = 16) | substr ($ cardnumber, 0, 1 )! = '4 ')

Return 0;
}
Elseif ($ cardtype = 'amx '){
If (strlen ($ cardnumber )! = 15 |! Ereg ('3 [47] ', $ cardnumber) return;
}
Elseif ($ cardtype = 'discover '){
If (strlen ($ cardnumber )! = 16 | substr ($ cardnumber, 0, 4 )! = '20140901') return 0;
}
Else {
// Invalid type entered
Return-1;
}


// Start MOD 10 checks

$ Dig = toCharArray ($ cardnumber );
$ Numdig = sizeof ($ dig );
$ IntIntJ = 0;
For ($ intI = ($ numdig-2); $ intI> = 0; $ intI-= 2 ){
$ Dbl [$ intIntJ] = $ dig [$ intI] * 2;
$ IntIntJ ++;
}
$ Dblsz = sizeof ($ dbl );
$ Validate = 0;
For ($ intI = 0; $ intI <$ dblsz; $ intI ++ ){
$ Add = toCharArray ($ dbl [$ intI]);
For ($ intIntJ = 0; $ intIntJ $ Validate + = $ add [$ intIntJ];
}
$ Add = '';
}
For ($ intI = ($ numdig-1); $ intI> = 0; $ intI-= 2 ){
$ Validate + = $ dig [$ intI];
}
If (substr ($ validate,-1, 1) = '0') return 1;
Else return 0;
}


// Takes a string and returns an array of characters

Function toCharArray ($ intInput ){
$ Len = strlen ($ intInput );
For ($ intIntJ = 0; $ intIntJ <$ len; $ intIntJ ++ ){
$ Char [$ intIntJ] = substr ($ intInput, $ intIntJ, 1 );
}
Return ($ char );
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.