Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery find credit card type

Tags:

jquery

I found this simple function to return the 4 most common credit card types.

Being new to jQuery, what jQuery plugin can I use to display the credit card type as a user types in a credit card number in a input field?

 function creditCardTypeFromNumber(num) {
   // first, sanitize the number by removing all non-digit characters.
   num = num.replace(/[^\d]/g,'');
   // now test the number against some regexes to figure out the card type.
   if (num.match(/^5[1-5]\d{14}$/)) {
     return 'MasterCard';
   } else if (num.match(/^4\d{15}/) || num.match(/^4\d{12}/)) {
     return 'Visa';
   } else if (num.match(/^3[47]\d{13}/)) {
     return 'AmEx';
   } else if (num.match(/^6011\d{12}/)) {
     return 'Discover';
   }
   return 'UNKNOWN';
 }

Thank you!

like image 888
floatleft Avatar asked Mar 07 '11 02:03

floatleft


3 Answers

$('#someTextBox').change(function() {
    $('#someOutput').text(creditCardTypeFromNumber($(this).val()));
});

This will output into some element with id="someOutput" the result of the text box which fires when the user changes the text in the element id="someTextBox".

like image 165
Daniel A. White Avatar answered Sep 24 '22 06:09

Daniel A. White


http://www.ihwy.com/labs/jquery-validate-credit-card-extension.aspx

http://docs.jquery.com/Plugins/Validation/Methods/creditcard

Plugins by default may not have the card validation as you type. To have realtime validation, you can bind a simple keyup() on the input field so validation is done after each key stroke.

like image 29
Hussein Avatar answered Sep 21 '22 06:09

Hussein


jQuery Credit Card Type Detector Plugin https://github.com/christianreed/Credit-Card-Type-Detector

like image 31
umpirsky Avatar answered Sep 24 '22 06:09

umpirsky