Restrict Space At First Position In Textbox Using Jquery/javascript
I have a situation where I need to restrict users from entering space in beginning of a TextBox. I am able to restrict space entry in TextBox. But not having any clues about not al
Solution 1:
keypress
event solution:
$("input").on("keypress", function(e) {
if (e.which === 32 && !this.value.length)
e.preventDefault();
});
Solution 2:
I tried but after writing something and moving the cursor to the first letter it allows a space there. This solution never allows entering a space character in the beginning of the text box.
$("input").on("keypress", function(e) {
var startPos = e.currentTarget.selectionStart;
if (e.which === 32 && startPos==0)
e.preventDefault();
});
Solution 3:
If you are using jQuery you can just call the trim method.
$.trim(' Hello World!'); // -> 'Hello World'
Note, this will remove all white space characters from the start and the end of your string.
Here is a demo using a button: http://jsfiddle.net/3Enr4/
Post a Comment for "Restrict Space At First Position In Textbox Using Jquery/javascript"