USB信用卡读卡器到CC领域

时间:2017-03-20 18:47:37

标签: javascript jquery credit-card

我有以下字段

<input type="number" id="input_6_cc_number" name="q6_invoiceAmount[cc_number]" class="form-textbox validate[required] cc_number" autocomplete="off" size="20" value="" data-component="cc_number" /> 

我正在使用此处的代码

$(document).ready(function(){

    $('#input_6_cc_number').click(function(){

        //document.getElementById('SwipeNowAlert').innerHTML = 'You may now swipe.';

    }).blur(function(){

        document.getElementById('SwipeNowAlert').innerHTML = 'Don\'t Swipe';

    }).focus(function(){

        document.getElementById('SwipeNowAlert').innerHTML = 'SWIPE NOW';

    }).keyup(function(event) {

        if (event.keyCode == 13) {

            var ccNum =  $('#credit-card-number').val();

            var isCaretPresent = false;
            var isEqualPresent = false;

            if (ccNum.indexOf("^") != -1)
                isCaretPresent = true
            else
                isCaretPresent = false;

            if (ccNum.indexOf("=") != -1)
                isEqualPresent = true
            else
                isEqualPresent = false;

            //handle parsing differently depending on card format
            if (isCaretPresent) {

                var cardData = ccNum.split('^');

                $("#first-name").val(formatFirstName(cardData[1]));
                $("#last-name").val(formatLastName(cardData[1]));

                var decryptedCardNumber = formatCardNumber(cardData[0]);

                $("#card-number").val(decryptedCardNumber);
                $("#card-type").val(getCardType(decryptedCardNumber));

                $("#expiration-month").val(cardData[2].substring(2, 4));
                $("#expiration-year").val(cardData[2].substring(0, 2));


            } else if (isEqualPresent) {

                var cardData = ccNum.split('=');

                var decryptedCardNumber = formatCardNumber(cardData[0]);

                $("#CardNumber").val(decryptedCardNumber);
                $("#CardType").val(getCardType(decryptedCardNumber));

                $("#ExpirationMonth").val(cardData[2].substring(2, 4));
                $("#ExpirationYear").val(cardData[2].substring(0, 2));
            }

        } else {
            return true;
        }
    }); 


    function formatCardNumber(cardNum) {

        var result = "";

        result = cardNum.replace(/[^0-9]*/, "");

        return result;
    }

    function formatFirstName(name) {

        if (name.indexOf("/") != -1) {

            var nameSplit = name.split('/');

            return nameSplit[1];

        } else {
            return "";
        }
    }

    function FormatLastName(name) {

        if (name.indexOf("/") != -1) {

            var nameSplit = name.split('/');

            return nameSplit[0];

        } else {
            return "";
        }
    }

    function getCardType(number) {

        var re = new RegExp("^4");
        if (number.match(re) != null)
            return "Visa";

        re = new RegExp("^(34|37)");
        if (number.match(re) != null)
            return "American Express";

        re = new RegExp("^5[1-5]");
        if (number.match(re) != null)
            return "MasterCard";

        re = new RegExp("^6011");
        if (number.match(re) != null)
            return "Discover";

        return "";
    }

});

我正在使用信用卡读卡器,我无法让它正确扫描。我正在使用JQuery 3.2。我无法显示点击功能,它应该提醒我滑动,不要滑动或现在滑动。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

假设读卡器用作键盘,当您按照自己的想法执行键盘事件时,它将起作用,但如果您使用 focusout()和focusin,则可能会显示您尝试显示的消息( )事件而非模糊()和焦点(),尝试一下让我知道。来自阿根廷的祝福。