Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8635027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:52:15+00:00 2026-06-12T09:52:15+00:00

I have a text input that I don’t want spaces in. If someone types

  • 0

I have a text input that I don’t want spaces in. If someone types a space or pastes text with a space, I would like to change the text back to what it was before the space was inputted AND also keep the caret position the same. Maybe there is an easier way to do this, but I can’t figure it out.

This is what I have so far:

<html><head><title>Test</title></head><body>

<input type=text id="inputText" value="testValue" onInput=doIt(this);>

</body></html>​

And the javascript that would be included (I used jsFiddle):

var editedValue = "testValue";

// alert(editedValue);

function doIt(that)
{

var caretPos = that.selectionStart;

if (that.value.indexOf(" ") != -1)
{
that.value = editedValue;
// alert(caretPos);
that.selectionStart = caretPos;
}
else
{
editedValue = that.value;
}

}​

It seems to all work, except for, if the caret is in the middle of the text, and you type or paste spaces, the caret does not return to the original position.

Can anyone help me figure this out? Or show me an entirely new/easier/simpler way to not allow spaces typed or pasted into a text input?

Here is the jsfiddle I was trying it with if it helps: http://jsfiddle.net/djSnL/4/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:52:17+00:00Added an answer on June 12, 2026 at 9:52 am

    http://jsfiddle.net/djSnL/5/

    You want to capture the onkeydown event:

    function doIt(e)
    {
        var e = e || event;
    
        if (e.keyCode == 32) return false; // 32 is the keycode for the spacebar
    
    }
    
    window.onload = function(){
        var inp = document.getElementById("inputText");
    
        inp.onkeydown = doIt;
    };
    

    Note that they will still be able to paste spaces into the input, so you’ll have to handle that as well.

    To handle pasting, it’s as simple as: http://jsfiddle.net/djSnL/7/

    function pasteIt(e)
    {
        var e = e || event;
    
        this.value = this.value.replace(/\s/g,'');
    
    }
    

    With both, you’ll get the desired effect.

    I’m not 100% certain, but I believe only webkit browsers will let you know what the data is before it’s pasted: http://jsfiddle.net/djSnL/8/

    function pasteIt(e)
    {
        var e = e || event;
        var data = e.clipboardData.getData("text/plain");
    
        if (data.match(/\s/g)) return false;    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input box that I don't want postback to occur on someone
I have a paragraph of text below that I want to use an input
I have a few simple html text inputs. I would like to check that
I have a standard text input that has a blur event. The blur event
I have an input text that is mapped to a Long property. private Long
I have a number of text input elements that when its values are changed,
In my code, I have two text boxes that validate input as the user
I have a jQuery jPicker colorpicker that is bound to an input text field
I have a form on my website that takes input from a text area
I have a linux utility that parses structured input ( a text file ),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.