JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" class="input-phone">
JavaScript
var inputs = document.querySelectorAll('.input-phone');
[].forEach.call(inputs, function(input) {
input.addEventListener('input', function (e) {
var target = e.target, position = target.selectionEnd, length = target.value.length;
target.value = target.value.replace(/[^\dA-Z]/g, '').replace(/(.{2})/g, '$1 ').trim();
target.selectionEnd = position += ((target.value.charAt(position - 1) === ' ' && target.value.charAt(length - 1) === ' ' && length !== target.value.length) ? 1 : 0);
});
});