How do I prevent a leading space from being entered in a textbox?
问题 I'm using jQuery 2. How do I prevent a space being entered in a textbox if it is a leading space (e.g. at the beginning of the string)? I thought this would do it $(document).on("keyup","#s" ,function(evt){ var firstChar = $("#s").val() if(evt.keyCode == 32 && firstChar){ $("#s").val( $("#s").val().trim() ) console.log(" called ") return false; } }) And basically it does but it is a little sloppy. You can see the space being entered and then removed and I was looking for something slightly