Skip to content

Instantly share code, notes, and snippets.

@wmantly
Created November 12, 2014 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmantly/a803a62a82613574e664 to your computer and use it in GitHub Desktop.
Save wmantly/a803a62a82613574e664 to your computer and use it in GitHub Desktop.
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