Created
June 15, 2013 00:12
-
-
Save stimms/5786201 to your computer and use it in GitHub Desktop.
Credit card type detection
This file contains 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
<html> | |
<body> | |
<label for="creditcard">Credit Card Number</label> | |
<input type="text" name="creditcard" id="creditcard"/> | |
<span id="creditcardname"></span> | |
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js"></script> | |
<script type="text/javascript"> | |
$(function(){ | |
$("#creditcard").keyup(function(){ | |
$("#creditcardname").text(""); | |
var creditCardNumber = $(this).val(); | |
if(creditCardNumber.length > 0) | |
{ | |
if(creditCardNumber[0]=="4") | |
{ | |
setCreditCardType("Visa"); | |
} | |
} | |
if(creditCardNumber.length > 1) | |
{ | |
if(creditCardNumber.search("34|37") == 0) | |
{ | |
setCreditCardType("American Express"); | |
} | |
if(creditCardNumber.search("31|52|53|54|55") == 0) | |
{ | |
setCreditCardType("MasterCard"); | |
} | |
if(creditCardNumber.search("62|88")==0) | |
{ | |
setCreditCardType("China UnionPay"); | |
} | |
} | |
if(creditCardNumber.length > 3) | |
{ | |
if(creditCardNumber.search(/2014|2149/) == 0) | |
{ | |
setCreditCardType("Diners Club enRoute"); | |
} | |
} | |
}); | |
}); | |
function setCreditCardType(type) | |
{ | |
$("#creditcardname").text("This is a " + type + " card"); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment