当前位置: 代码迷 >> Web前端 >> Web前端银行卡号分段输入成效(如:6225 7687 0971 2222)
  详细解决方案

Web前端银行卡号分段输入成效(如:6225 7687 0971 2222)

热度:156   发布时间:2012-10-06 17:34:01.0
Web前端银行卡号分段输入效果(如:6225 7687 0971 2222)

?

// 输入银行卡号时的分段处理效果(如:6225 7687 0971 2222)
function convertSpace(obj)
{
    var val = $.trim(obj.value).replace(/\s/g, '');

    var val2 = "";
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        var ch = val.charAt(i);
        if(ch >= 0 && ch <= 9) {
            val2 += ch;
            ++len;
            if (len % 4 == 0) {
                val2 += ' ';
            }
        }
    }
    
    if(val2.substring(val2.length-1,val2.length)== ' ')
    {
        val2 = val2.substring(0,val2.length-1)
    }
    
    if(obj.value != val2) {
        var pos = getPositionForInput(obj);
        obj.value = val2;
        if(pos != val2.length) {
            if(val2.charAt(pos - 1) == ' ') {
                pos++;
            }
            setPositionForInput(obj, pos);
        }
    }
}
  相关解决方案