I have written a simple javascript code to remove whitespace from a text box. This is working fine in firefox. It is even working in IE and Chrome also, but there is a little problem.
After giving the value in text box if i am trying to move my cursor to the left side of the text box using the left arrow key from my keyboard, it is only crossing the first character from the right side and then stopping.
here is my code:
<script>
function fixme(element)
{
if(element.value != '')
{
var val = element.value;
var pattern = new RegExp('[ ]+', 'g');
val = val.replace(pattern, '');
element.value = val;
}
}
</script>
<input type="text" onkeydown="fixme(this)" onblur="fixme(this)"/>
Any idea?
Revamped your function to change the text input’s value property only when the fixed value is different than the one entered, give it a try.
Fiddle
But if your users are not supposed to type white space in the text input, you may simply prevent them from doing so:
Keep your previous function in case the user pastes/drags text over to the text input.
Fiddle