简体   繁体   中英

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?

<script language="javascript">

String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
$(function(){
    $("#idOfTextBoxHere").keyup(function(){
      $(this).val( $(this).val().ltrim());
    });
});

</script>

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);


        });

Use the regular expression

/^\ //

to remove the space at the beginning of the line.

use the trim() function

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

Try:

$("#idOfTextboxHere").focusout(function(){
    $(this).val( $.trim($(this).val()) );
});

When the user clicks away, it will trim the beginning and end of the textbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM