How to make my code shorter. This Code formats Credit card number. Vanilla JS How to make my script shorter and better? This code allows only digits, auto-format card number like this XXXX XXXX XXXX XXXX. if user hits delete or backspace to delete space in between digits, nothing happens caret just moves 1 space forward or backward, it depends on which key user pressed (delete or backspace). If user hits backspace after digit and the previous second symbol is space then backspace deletes the digit and moves 2 spaces back like this (XXXX X|XXX XXXX XXXX -&gt; XXXX| XXXX XXXX XXX ). Delete key dose the same but vice versa like this (XXX|X XXXX XXXX XXXX -&gt; XXXX |XXXX XXXX XXX ). It also allows ctrl+c,ctrl+v,ctrl+a,ctrl+x.


let input = document.querySelector('input');

input.addEventListener('keyup', function (e) {
    let cursor = input.selectionStart;
    let inpVal = input.value;
    let cardCode = inpVal.replace(/\s/g, '').substring(0, 16);
    cardCode = cardCode != '' ? cardCode.match(/.{1,4}/g).join(' ') : '';
    input.value = cardCode;
    let key = e.keyCode || e.which;
    // let ctrl = e.ctrlKey || key === 17 || e.metaKey;
    if ((key &gt;= 48 &amp;&amp; key &lt;= 57) || (key &gt;= 96 &amp;&amp; key &lt;= 105)) {
        if (cursor &lt; parseInt(inpVal.length)) {
            input.selectionEnd = cursor;
        }
    } else if (key == 8) {
        if (input.value[cursor - 1] == ' ') {
            input.selectionEnd = cursor - 1;
        } else {
            input.selectionEnd = cursor;
        }
    } else if (key == 46) {
        if (input.value[cursor + 1] == ' ') {
            input.selectionEnd = cursor + 2;
        } else {
            input.selectionEnd = cursor;
        }
    }
});
input.addEventListener('keydown', function (e) {
    let inpVal = input.value;
    let cursor = input.selectionStart;
    let key = e.keyCode || e.which;
    let ctrl = e.ctrlKey || key === 17 || e.metaKey;
    if (
        key == 9 ||
        // Ctrl+A
        (key == 65 &amp;&amp; ctrl === true) ||
        // Ctrl+C
        (key == 67 &amp;&amp; ctrl === true) ||
        // Ctrl+X
        (key == 88 &amp;&amp; ctrl === true) ||
        // Ctrl+V
        (key == 86 &amp;&amp; ctrl === true) ||
        // home, end, влево, вправо
        (key &gt;= 35 &amp;&amp; key &lt;= 40) ||
        (key &gt;= 48 &amp;&amp; key &lt;= 57) ||
        (key &gt;= 96 &amp;&amp; key &lt;= 105)
    ) {
        return;
    } else if (key == 8) {
        if (input.value[cursor - 1] == ' ' &amp;&amp; cursor &lt; parseInt(inpVal.length)) {
            e.preventDefault();
            input.selectionEnd = cursor - 1;
        }
    } else if (key == 46) {
        if (input.value[cursor] == ' ' &amp;&amp; cursor &lt; parseInt(inpVal.length)) {
            e.preventDefault();
            input.selectionStart = cursor + 1;
        }
    } else {
        e.preventDefault();
    }
});
&lt;form method="GET" action="#" class="card"&gt;
                &lt;label style="font-size: 20px; display: block; padding: 10px" for="cardnum"&gt;Credit Card number&lt;/label&gt;
                &lt;input style="font-size: 20px; padding: 10px" autocomplete="off" id="cardnum" name="cardnum" type="text" maxlength="19" title="Please enter a card number in XXXX XXXX XXXX XXXX format" pattern="^\d{4}\s\d{4}\s\d{4}\s\d{4}$" inputmode="numeric" required /&gt;
            &lt;/form&gt;
I've mainly got three points:

The whole thing should be wrapped in a function with the reference to the input element passed in. Currently it is hard coded to (only) apply to the first input on the page, which doesn't seem like a practical scenerio.

Instead of using the deprecated/non-standard keyCode and which, you should be using key. And if you do use keyCode you should define all code numbers as constants.

Unless I'm missing some side-effect, the use of parseInt on inpVal.length is pointless.