Reputation: 389
I want to remove spaces on the beginning of text on textbox on keyup event but will not remove spaces if it is between text. how can i do that?
Upvotes: 0
Views: 2732
Reputation: 1186
Hi i don know your exact need ple correct me
$('#your textbox id').keyup(function () {
var val = $(this).val();
val = val.replace(/^\s+/, '');
$(this).val(val);
});
Upvotes: 1
Reputation: 18308
<script language="javascript">
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
$(function(){
$("#idOfTextBoxHere").keyup(function(){
$(this).val( $(this).val().ltrim());
});
});
</script>
Upvotes: 2
Reputation: 6683
Try:
$("#idOfTextboxHere").focusout(function(){
$(this).val( $.trim($(this).val()) );
});
When the user clicks away, it will trim the beginning and end of the textbox.
Upvotes: 0
Reputation: 2795
use the trim()
function
var text = " hel lo ";
alert(text.trim());
Upvotes: 0
Reputation: 472
Use the regular expression
/^\ //
to remove the space at the beginning of the line.
Upvotes: 0