js卡自动检测keyinput

时间:2014-11-26 12:46:09

标签: javascript

我获得了以下正则表达式来构建卡片自动检测原型:

HWVisaElectronRegEx = @" ^(4026 | 417500 | 4508 | 4844 | 4913 | 4917)[0-9] +&#34 ;;

HWMasterCardRegEx = @" ^ 5 [1-5] [0-9] {14}";

我已经构建了以下脚本:

    var opacity = 'greyOut';

    function greyOut(el){
        $('p').not(el).addClass(opacity);
    }

    function getCreditCardType(accountNumber){

        //start without knowing the credit card type
        var result = "unknown";

        if (/^4(?:[0-9]{12}|[0-9]{15})/.test(accountNumber))
        {
            result = "HWVisaRegEx";
            greyOut($('.HWVisaRegEx'));
        }

        else if (/^5[1-5][0-9]{14}/.test(accountNumber))
        {
            result = "mastercard";
            greyOut($('.mastercard'));
        }

        return result;

    }

    $(document).ready(function(){

        var input = $('.card');

        input.keyup(function(){

            if(input.val() === ''){
                $('p').removeClass(opacity);
            }

            getCreditCardType(input.val());

            if(input.val() === ""){
              $('p').removeClass(opacity);
            }

        });


    });

这是html:

<label>Card Number</label><input type="number" class="card"/>

只有在您输入完整的卡号后才能检测到卡,它是如何工作的,以便在只有1个或最多2个数字后检测到卡?

0 个答案:

没有答案