|
$(document).ready(function () { |
|
|
|
// buttons to populate test credit card numbers into |
|
// input box |
|
$('#testVisa').click(function () { |
|
$('#ccNum').val('4111111111111111'); |
|
}); |
|
$('#testVisaShort').click(function () { |
|
$('#ccNum').val('4222222222222'); |
|
}); |
|
$('#testMasterCard').click(function () { |
|
$('#ccNum').val('5555555555554444'); |
|
}); |
|
$('#testAmex').click(function () { |
|
$('#ccNum').val('378282246310005'); |
|
}); |
|
$('#testDiscover').click(function () { |
|
$('#ccNum').val('6011111111111117'); |
|
}); |
|
$('#testJCB').click(function () { |
|
$('#ccNum').val('3530111333300000'); |
|
}); |
|
$('#testDiners').click(function () { |
|
$('#ccNum').val('30569309025904'); |
|
}); |
|
|
|
|
|
$('#ccValidator').click(function () { |
|
initCardNumberValidity(); |
|
var ccNum = $('#ccNum').val(); |
|
// Remove any dashes in number |
|
ccNum = ccNum.split("-").join(""); |
|
ccNum = cardtype(ccNum); |
|
displayValidationMessage(ccNum); |
|
|
|
}); |
|
|
|
function displayValidationMessage(s) { |
|
$('#ccValidation').text(s); |
|
var valid = $('#validCardNumber').val(); |
|
} |
|
|
|
function initCardNumberValidity() { |
|
$('#validCardNumber').val('invalid'); |
|
$('#cardType').val('none'); |
|
} |
|
|
|
function cardNumberIsValid(cardType) { |
|
$('#validCardNumber').val('valid'); |
|
$('#cardType').val(cardType); |
|
} |
|
|
|
function cardtype(number) { |
|
var re = { |
|
visa: /^4[0-9]{12}(?:[0-9]{3})?$/, |
|
mastercard: /^5[1-5][0-9]{14}$/, |
|
amex: /^3[47][0-9]{13}$/, |
|
diners: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, |
|
discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/, |
|
jcb: /^(?:2131|1800|35\d{3})\d{11}$/ |
|
}; |
|
if (re.visa.test(number)) { |
|
cardNumberIsValid('visa'); |
|
return 'Valid Credit Card Number format: Visa'; |
|
} else if (re.mastercard.test(number)) { |
|
cardNumberIsValid('mastercard'); |
|
return 'Valid Credit Card Number format: Mastercard'; |
|
} else if (re.amex.test(number)) { |
|
cardNumberIsValid('amex'); |
|
return 'Valid Credit Card Number format: Amex'; |
|
} else if (re.discover.test(number)) { |
|
cardNumberIsValid('discover'); |
|
return 'Valid Credit Card Number format: Discover'; |
|
} else if (re.diners.test(number)) { |
|
return 'We accept Visa, Mastercard, American Express and Discover Card. Please try another card. (You entered a Diners Club Card.)'; |
|
} else if (re.jcb.test(number)) { |
|
return 'We accept Visa, Mastercard, American Express and Discover Card. Please try another card. (You entered a JCB Card.)'; |
|
} |
|
return 'The card number you entered is invalid. Please enter a valid Visa, Mastercard, American Express, or Discover card number.'; |
|
} |
|
|
|
}); |