I'm trying to prevent users from inputting repeat spaces in a textarea. Any spaces in a row, I just want the textarea to ignore.
I am trying to simply replace repeats with a single space in my onchange handler by doing this: value.replace(/\s+/g, ' ')
This does work, however, the caret always moves to the end of the textarea. So if I place the caret in the middle of a sentence in a textarea and press space twice, the caret moves to the end of the textarea which is annoying to the user.
I've found I can use event.preventDefault() in the onkeypress handler and it doesn't move the cursor, however, onkeypress does not provide the new value of the input, so I don't know if there are repeat spaces.
Any ideas on how to handle this?