How to insert space every 4 characters for IBAN registering?

前端 未结 9 1713
感情败类
感情败类 2020-11-29 03:28

I\'m really new in JavaScript and I would like to add to my input text, space insertion for IBAN account registering.

相关标签:
9条回答
  • 2020-11-29 04:08

    I wrote a simple function extending David's function to handle the last space. Also you can specify the separator.

    function spacify(str, after, c) {
        if (!str) {
            return false;
        }
        after = after || 4;
        c = c || " ";
        var v = str.replace(/[^\dA-Z]/g, ''),
            reg = new RegExp(".{" + after + "}", "g");
        return v.replace(reg, function (a) {
            return a + c;
        }).replace(/[^0-9]+$/, "");
    }
    console.log(spacify("123123123131",4," "))
    console.log(spacify("12312312313",4,"-"))

    0 讨论(0)
  • 2020-11-29 04:08

    This is the shortest version using JQuery on input with type number or tel:

    $('input[type=number], input[type=tel]').on('input', function (e) {
         e.target.value = e.target.value.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim();
    });
    

    You can also change the 4 to any other character limit you want.

    0 讨论(0)
  • 2020-11-29 04:10

    The code from Josh Crozie is really nice, but not complete.

    Two issues with it;

    • If the caret is not at the end but e.g. at the before last position and the user starts typing, sometimes the caret doesn't stay at the before last position
    • Another issue is with Android 7+ devices. Those devices update the caret position slightly later, that means it needs a setTimeout() before reading the caret location

    The code below is based on the code of Josh Crozie, now with the two issues mentioned above fixed and a little more verbose for readability purpose:

    var isAndroid = navigator.userAgent.indexOf("ndroid") > -1;
    var element = document.getElementById('iban');
    
    element.addEventListener('input', function () {
        if (isAndroid) {
            // For android 7+ the update of the cursor location is a little bit behind, hence the little delay.
            setTimeout(reformatInputField);
            return;
        }
        reformatInputField();
    });
    
    function reformatInputField() {
        function format(value) {
            return value.replace(/[^\dA-Z]/gi, '')
                .toUpperCase()
                .replace(/(.{4})/g, '$1 ')
                .trim();
        }
        function countSpaces(text) {
            var spaces = text.match(/(\s+)/g);
            return spaces ? spaces.length : 0;
        }
    
        var position = element.selectionEnd;
        var previousValue = element.value;
        element.value = format(element.value);
    
        if (position !== element.value.length) {
            var beforeCaret = previousValue.substr(0, position);
            var countPrevious = countSpaces(beforeCaret);
            var countCurrent = countSpaces(format(beforeCaret));
            element.selectionEnd = position + (countCurrent - countPrevious);
        }
    }
    <label for="iban">iban</label>
    <input id="iban" type="text" name="iban" size="35" />

    0 讨论(0)
提交回复
热议问题