输入银行卡号,每四位数后边加一空格

第一种很普通的方法:

<input type="text" id="waterAcount" onkeyup='checkNumAddSpace(this.value)'/>
function checkNumAddSpace(num){

 if(num.length == 4 || num.length == 9 || num.length ==14 || num.length ==19){

 $("waterAcount").value = num + " ";

 }

}

第二种方法,利用正则表达式:

<input type="text" id="waterAcount"/>
window.onload =function() {

            document.getElementById("waterAcount").onkeyup =function() {

                this.value =this.value.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 ");;

            };

        };
posted on 2013-05-09 08:47  老茶壶  阅读(767)  评论(0编辑  收藏  举报