USB Credit Card reader to CC fields

0

I have the following field

<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" /> 

And I’m using the code here

$(document).ready(function(){

    $('#creditCardNumber').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 "";
    }

});

I’m using a Credit Card reader and I cannot get it to scan right. I’m using JQuery 3.2 for this. I cannot get the click function to appear either which should alert me to swipe, don’t swipe, or swipe now. Thanks for any help

Link: USB Credit Card reader to CC fields
Source: Stack Jquery

Share.

About Author

Leave A Reply