简体   繁体   中英

How to remove double spaces in between texts on html text input

I'm trying to figure out, that how to remove the double spaces which are placed in-between texts in the HTML Text field. [For example: Tom and Jerry - in here, after "Tom" there are two spaces are placed]. I need to remove or replace the double spaces and place a single space only while keying in the HTML text box. Any Help on this? ... Thanks

You might add a keyup listener, which, when triggered, uses a regular expression to replace all double-spaces (or more) with single spaces:

 const input = document.querySelector('input'); input.addEventListener('keyup', () => { input.value = input.value.replace(/ +/g, ' '); }); 
 <input> 

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