rjx44
rjx44

Reputation: 389

Remove space on textbox but not between words

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

Answers (5)

run
run

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

Joshua Enfield
Joshua Enfield

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

Stefan H
Stefan H

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

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

use the trim() function

var text = "   hel  lo ";
alert(text.trim());

Upvotes: 0

colechristensen
colechristensen

Reputation: 472

Use the regular expression

/^\ //

to remove the space at the beginning of the line.

Upvotes: 0

Related Questions