Good morning everybody!
I have a case, when I should prevent users to entering space as a first character on input field.
I have a demo here: http://jsbin.com/fo
Try something like
$('input').keyup(function () {
if($(this).val() == '') {
if(event.keyCode == 32) {
return false;
}
}
}
This would check for current value, and then it would run the function. Keyup is used, to check for the value after the key has been unpressed. If you use keydown, it would still have the value. Key up would get the value after the key press event has passed.
Or as others has told you to always keep trimming the value. You can use jQuery trim()
method to trim the value, regardless of whether user inputs space character or not.
$('input').keyup(function () {
$(this).val($.trim($(this).val()));
}
This would add the trimmered form of the value of the current input field.