I see 2 solutions :
CSS solution
Like said in comment, you could slightly increase the with of your input to prevent this 'jump' behavior
like : width : 202px
fiddlejs
JavaScript solution
If you can't/ don't want to change the width of your input you can prevent the keypress event, then check the length of the input value. If it less than 4 add it else do nothing.
Jquery way:
var t = $('#input-form');
t.keypress( function(event){
//Prevent the value to be added
event.preventDefault();
//Regex that you can change for whatever you allow in the input (here any word character --> alphanumeric & underscore)
var reg = /\w/g;
//retreive the key pressed
var inputChar = String.fromCharCode(event.which);
//retreive the input's value length
var inputLength = t.val().length;
if ( reg.test(inputChar) && (inputLength < 4) ) {
//if input length < 4, add the value
t.val(t.val() + inputChar);
}else{
//else do nothing
return;
}
});
fiddlejs
Pure JavaScript:
var t = document.getElementById('input-form');
t.addEventListener('keypress', function(event){
//Prevent the value to be added
event.preventDefault();
//Regex that you can change for whatever you allow in the input (here any word character --> alphanumeric & underscore)
var reg = /\w/g;
//retreive the input's value length
var inputChar = String.fromCharCode(event.which);
//retreive the input's value length
var inputLength = t.value.length;
if ( reg.test(inputChar) && (inputLength < 4) ) {
//if input length < 4, add the value
t.value = t.value + inputChar;
}else{
//else do nothing
return;
}
});
fiddlejs
inputslightly wider so that there's space for the carat after the fourth character: jsfiddle.net/n23dfs4t/1