How to disable/remove only first space on input field?

前端 未结 4 1268
萌比男神i
萌比男神i 2021-02-11 03:15

Good morning everybody!

I have a case, when I should prevent users to entering space as a first character on input field.

I have a demo here: http://jsbin.com/fo

4条回答
  •  忘了有多久
    2021-02-11 03:37

    This jQuery function selects all the text inputs when a key is unpressed, removes all whitespace from the beginning of the string and replaces the value of the input:

     
    

    I do not recommend to use trim() when blank spaces are replaced directly with keyup() because when you type a space after a word, it will be deleted before you start writing the next word.

提交回复
热议问题