1.获取不同浏览器按下的键码

$(document).ready(function(){
   $("#keydown").keydown(function(event){
        var keycode = (event.keyCod ? event.keyCode : event.which);
        console.log("keycode:" + keycode);
        }
    }) 
})    

2.输入卡号卡号规则***** **** **** ****** 中间有空格

$('#cardno').on('input', function () {
        //去掉所有空格
        this.value = this.value.replace(/\s/g, "");

        if (/^[0-9]*[1-9][0-9]*$/.test(this.value)) {
            var len = this.value.length;
            if (len >= 6 && len <=9) {
                this.value = this.value.substring(0, 5) + " " + this.value.substring(5)
            } else if (len >= 10 && len <=13) {
                this.value = this.value.substring(0, 5) + " " + this.value.substring(5, 9) + " " + this.value.substring(9)
            } else if (len >= 14 && len <=20) {
                this.value = this.value.substring(0, 5) + " " + this.value.substring(5, 9) + " " + this.value.substring(9, 13) + " " + this.value.substring(13)
            }
        }
    })

 

posted on 2020-03-19 14:11  忆夏KhaZix  阅读(93)  评论(0编辑  收藏  举报