1

I want to prevent 2 spaces from being entered consecutively. Right now, for a name, you can use all spaces. I want multiple words with 1 space in-between each word, but not more than 1 space together. Also, possibly even prevent multiple spaces if the text was copied and pasted in?

I force 3 characters but they can all be spaces in my form unless I use this code but it stops all spaces. I want to allow only 1 space between words. :

<input type="text" name="name" placeholder="Required...." 
  minlength="3" maxlength="40" required 
  oninvalid="this.setCustomValidity('Please enter a name with at least 3 characters.')" 
  oninput="setCustomValidity('')" 
  onKeyDown="javascript: var keycode = keyPressed(event); if(keycode==32){ return false; }" 
/>

I found a way to stop all spaces but I lost the code trying other things. I could find it again but it is not exactly what I want. I know there must be a simple way, and I can't be the only one who wants it. Can someone please help me?

5
  • 1
    Does this answer your question? Regex to replace multiple spaces with a single space Commented Jun 6, 2023 at 9:26
  • Mine it's just an example, if you try to input/paste double character you will see the script doesn't allow. (we r not coding service) Commented Jun 7, 2023 at 12:39
  • I got it to erase spaces using the link above! :D Thanks! but it affects the 3 character minimum for input (sucks for the name) they can keep pressing the space and it counts the spaces it removes. any idea how can I fix that? Commented Jun 7, 2023 at 14:05
  • the code I used is in the "Comprehensive unencrypted answer for newbies et al" answer Commented Jun 7, 2023 at 14:18
  • even though I can't get the consecutive spaces to be prevented without counting the spaces towards the forms minlength, I still made a solid form that works really well using 2 names disabling all spaces which does not mess up the count either. I used the no space combined with the following simple html code in the input to force a valid email: pattern="^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$" now if I could force no consecutive spaces using a similar code? can anyone help me Commented Jun 8, 2023 at 5:03

1 Answer 1

1

As you can see from my example the code i linked work with oninput too

document.querySelector('input').addEventListener('input', (e) => {
   e.target.value = e.target.value.replace( /  +/g, ' ' );
});
document.querySelector('input').addEventListener('change', (e) => {
   e.target.value = e.target.value.replace( /  +/g, ' ' );
});
<input type="text">

2
  • This is what I did looking at that reply and it is not working. <script language="javascript" type="text/javascript"> function ignore2spaces(e) { document.querySelector('input').addEventListener('input', (e) => { e.target.value = e.target.value.replace( / +/g, ' ' ); }); document.querySelector('input').addEventListener('change', (e) => { e.target.value = e.target.value.replace( / +/g, ' ' ); })}; </script> onkeypress="return ignore2spaces(event);" Commented Apr 22 at 15:31
  • You don't need to use onkeypress with my code. Just use my event like my example Commented Jun 10 at 8:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.