Skip to content

Instantly share code, notes, and snippets.

@stimms
Created June 15, 2013 00:12
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 stimms/5786201 to your computer and use it in GitHub Desktop.
Save stimms/5786201 to your computer and use it in GitHub Desktop.
Credit card type detection
<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