Advertisement
Guest User

jquery.creditcardtypes.js

a guest
Feb 29th, 2012
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.     $.getCreditCardType = function(val) {
  3.       if(!val || !val.length) return undefined;
  4.      
  5.       switch(val.charAt(0)) {
  6.         case '4': return 'VISA';
  7.         case '5': return 'MC';
  8.         case '3': return 'AMEX';
  9.         case '6': return 'DISCOVER';
  10.       };
  11.      
  12.       return undefined;
  13.     };
  14.    
  15.     $.fn.creditCardType = function(options) {
  16.       var settings = {
  17.           target: '#credit_card_type',
  18.       };
  19.              
  20.       if(options) {
  21.           $.extend(settings, options);
  22.       };
  23.      
  24.       var keyupHandler = function() {
  25.         var background_position = '0 0';
  26.         switch($.getCreditCardType($(this).val())) {
  27.           case 'VISA':
  28.             background_position = '0 -23px';
  29.             break;
  30.           case 'MC':
  31.             background_position = '0 -46px';
  32.             break;
  33.           case 'AMEX':
  34.             background_position = '0 -69px';
  35.             break;
  36.           case 'DISCOVER':
  37.             background_position = '0 -92px';
  38.             break;
  39.         };
  40.         $(settings.target).css('background-position', background_position);
  41.       };
  42.                          
  43.       return this.each(function() {
  44.         $(this).bind('keyup',keyupHandler).trigger('keyup');
  45.       });
  46.     };
  47.    
  48. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement