Created
November 12, 2014 22:29
-
-
Save wmantly/a803a62a82613574e664 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreditCard( ccNum ){ | |
this.ccNum = ccNum; | |
this.cardType = "INVALID" | |
this.valid = false | |
this.cardType = this.validStartingNums(); | |
if( !this.checkLength() ) return false ; | |
if( !this.validate() ) return false ; | |
} | |
CreditCard.prototype.validStartingNums = function(){ | |
if( this.ccNum.match(/^5[1-5]/i) ){ | |
return 'MASTERCARD'; | |
}else if( this.ccNum.match(/^3[4,7]/i) ){ | |
return 'AMEX'; | |
}else if( this.ccNum.charAt(0) == '4' ){ | |
return 'VISA'; | |
}else if( this.ccNum.slice(0,4) == "6011"){ | |
return "DISCOVER" ; | |
}else{ | |
return 'INVALID' ; | |
} | |
} | |
CreditCard.prototype.checkLength = function(){ | |
switch(this.ccNum.length){ | |
case 15: | |
if( this.cardType !== 'AMEX' ) break ; | |
case 16: | |
if( this.cardType == 'INVALID') break ; | |
return true ; | |
break ; | |
default: | |
return false ; | |
} | |
return false ; | |
} | |
CreditCard.prototype.validate = function(){ | |
var testNum = this.ccNum.split("").reverse().join("") ; | |
var out = 0 ; | |
for( var i = 0; i < testNum.length; i++){ | |
if( i % 2 == 0 ) continue ; | |
out += Number( testNum[i] ); | |
} | |
console.log(out % 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment