Home > database >  How to disable space in input type="text"
How to disable space in input type="text"

Time:01-30

Can someone show me an example of how i can edit the below code to disable or remove space when typing in input type="text field.

<input type="text" id="username" name="username"  placeholder="Username">

I tried to to find a similar script but to no avail.

CodePudding user response:

You can disable the spaces in the input field like this:

<input
  type="text"
  id="username"
  name="username"
  
  placeholder="Username"
  onkeypress="return event.charCode != 32"
/>

The onkeypress event handler checks the ASCII code of the character being typed, and if it's 32 (the ASCII code for the space character), it returns false, which means the space won't be entered into the input field.

  •  Tags:  
  • html
  • Related