JSFiddle - React, Tailwind, and code Playground
by Alexis LĂłpez Espinoza
HTML
<input type="text" id="foo" value="+569" autofocus />
JavaScript
var foo = document.querySelector("#foo");
foo.addEventListener("keydown", function(event){
if ((event.which == 8 && this.value.length < 5) || (event.which == 46 && this.selectionStart < 4) || (event.which != 37 && event.which != 39 && this.selectionStart < 4)){
event.preventDefault();
}
}, false);
foo.addEventListener("input", function(){
this.value = this.value.replace(/[^0-9\+]/g, "");
this.value = this.value.substr(0, 4) + this.value.substr(4).replace(/\++/g, "");
}, false);