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);
        }
    }
}

你可能感兴趣的:(Web)