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

前端 未结 3 1700
耶瑟儿~
耶瑟儿~ 2021-02-11 03:29

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

3条回答
  •  -上瘾入骨i
    2021-02-11 03:33

    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.

提交回复
热议问题