'How to insert a space after user types the slash character (/) inside a content editable input field?
From what I have seen so far here on stackoverflow, there are some recommendations about the String.replaceAll() function, or using javascript with the input ID. The code below will introduce a space after five characters, but I need it to add the space after the slash character is typed.
document.getElementById('target').addEventListener('input', function (e) {
var target = e.target, position = target.selectionEnd, length = target.value.length;
target.value = target.value.replace(/\s/g, '').replace(/(\d{5})/g, '$1 ').trim();
target.selectionEnd = position += ((target.value.charAt(position - 1) === ' ' && target.value.charAt(length - 1) === ' ' && length !== target.value.length) ? 1 : 0);
});
<label id="target" contenteditable="true" type="text"/>someText</label>
I appreciate any guidance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
