jquery - Automatically Determine Credit Card Type -
i'm trying automatically determine credit card type. code below works, not on mobile. i've tried finding solution mobile, i'm unable @ time.
here's jquery:
(function($) { $.getcreditcardtype = function(val) { if(!val || !val.length) return undefined; switch(val.charat(0)) { case '4': return 'visa'; case '5': return 'mastercard'; case '3': return 'amex'; case '6': return 'discover'; }; return undefined; }; $.fn.creditcardtype = function(options) { var settings = { target: '#credit-card-type', }; if(options) { $.extend(settings, options); }; var keyuphandler = function() { $("#credit-card-type li").removeclass("active"); $("input[id=authorizenet_cc_type]").val(''); switch($.getcreditcardtype($(this).val())) { case 'visa': $('#credit-card-type .vi').addclass('active'); $("input[id=authorizenet_cc_type]").val('vi'); break; case 'mastercard': $('#credit-card-type .mc').addclass('active'); $("input[id=authorizenet_cc_type]").val('mc'); break; case 'amex': $('#credit-card-type .ae').addclass('active'); $("input[id=authorizenet_cc_type]").val('ae'); break; case 'discover': $('#credit-card-type .di').addclass('active'); $("input[id=authorizenet_cc_type]").val('di'); break; }; }; return this.each(function() { $(this).bind('keyup',keyuphandler).trigger('keyup'); }); }; })(jquery);
answer: added
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
i give credit scott provided.
have tried using keypress instead of keyup?
$(this).keypress(function() { });
or using input event of input field monitoring?
$(this).on('input', function() { });
edit - answering question
i wasn't sure going on in code out of context. changed few things in scoping things working.
first off, going assume html looks this.
<ul id="credit-card-type"> <li> <form> <input type="text" id="credit-card-type" /> <input type="text" id="authorizenet_cc_type" /> </form> </li> </ul>
then js using input event
var getcreditcardtype = function(val) { if(!val || !val.length) return undefined; switch(val.charat(0)) { case '4': return 'visa'; case '5': return 'mastercard'; case '3': return 'amex'; case '6': return 'discover'; } return undefined; } var keyuphandler = function() { console.log('test') $("#credit-card-type li").removeclass("active"); $("input[id=authorizenet_cc_type]").val(''); switch(getcreditcardtype($(this).val())) { case 'visa': $('#credit-card-type .vi').addclass('active'); $("input[id=authorizenet_cc_type]").val('vi'); break; case 'mastercard': $('#credit-card-type .mc').addclass('active'); $("input[id=authorizenet_cc_type]").val('mc'); break; case 'amex': $('#credit-card-type .ae').addclass('active'); $("input[id=authorizenet_cc_type]").val('ae'); break; case 'discover': $('#credit-card-type .di').addclass('active'); $("input[id=authorizenet_cc_type]").val('di'); break; }; }; $('input#credit-card-type').on('input',keyuphandler);
Comments
Post a Comment