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 8393369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:45:55+00:00 2026-06-09T19:45:55+00:00

I have written a simple javascript code to remove whitespace from a text box.

  • 0

I have written a simple javascript code to remove whitespace from a text box. This is working fine in firefox. It is even working in IE and Chrome also, but there is a little problem.
After giving the value in text box if i am trying to move my cursor to the left side of the text box using the left arrow key from my keyboard, it is only crossing the first character from the right side and then stopping.
here is my code:

<script>
function fixme(element)
{
    if(element.value != '')
    {
        var val = element.value;
        var pattern = new RegExp('[ ]+', 'g');
        val = val.replace(pattern, '');
        element.value = val;
    }
}
</script>

<input type="text" onkeydown="fixme(this)" onblur="fixme(this)"/>

Any idea?

  • 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-09T19:45:57+00:00Added an answer on June 9, 2026 at 7:45 pm
    function fixme(element) {
        if(element.value != '') {
            var val = element.value;
            val = val.replace(/\s/g, '');
            if (val != element.value) element.value = val;
        }
    }
    
    <input type="text" onkeyup="fixme(this)" onblur="fixme(this)" />
    

    Revamped your function to change the text input’s value property only when the fixed value is different than the one entered, give it a try.

    Fiddle

    But if your users are not supposed to type white space in the text input, you may simply prevent them from doing so:

    function nospace(e) {
        e = e || window.event;
        return e.keyCode !== 32;
    }
    
    <input type="text" onkeydown="return nospace(event)" onkeyup="fixme(this)" onblur="fixme(this)" />
    

    Keep your previous function in case the user pastes/drags text over to the text input.

    Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written simple code to get content from xml file to php. $xml
I have written a code in javascript. The code works fine in IE, Chrome
I have a problem with a very simple piece of code written in Javascript,
I have written a simple web Application which is running(on Tomcat Server) fine on
I have written a simple jQuery.ajax function which loads a user control from the
I have two simple textareas where in i want to highlight the javascript code
This is a simple scraper written in JavaScript with Node.js, for scraping Wikipedia for
I have written a quite simple javascript method, which should send an ajax request
I have a written a simple code which would display the location on the
I have written a very simple Chrome extension. It consists of this background page:

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.