Solved

javascript  auto remove space from form entry

Posted on 2004-10-18
3
466 Views
Last Modified: 2008-02-01
Using javascript and HTML, how could you do the following?

I have a text field input box in a html form. The size limit is 16.
What I want it to do is if a user enters in a space, I want javascript to remove it immediately as the input box will not allow spaces.
0
Comment
Question by:cimmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 12338587
Here do this


function ValidKeys( e )
{
     // Invalid key code
      var intInvalidKeyCode = 20; // Space
      var intKeyPressed = 0;
      var blnResult = true; // Assume OK
      
      // IE or Netscape?
      if( document.all )
      {
            // Ie
            intKeyPressed = event.keyCode;
      }
      else
      {
            // netscape
            intKeyPressed = e.which
      }
            
      // Did they press space?
      if( intKeyPressed == intInvalidKeyCode )
      {
            // Yes
            blnResult = false;
      }

      // Return Result
      return blnResult;
}


then in your text box put
onkeypress="return ValidKeys( );"
0
 
LVL 15

Assisted Solution

by:justinbillig
justinbillig earned 66 total points
ID: 12338614
or if you want maximum functionality ( multiple invalid keys )


function ValidKeys( e )
{
     // Invalid key code
      var aintInvalidKeys = new Array( /* Comma seperated list of Invalid Key Codes */ );
      var intIndex
      var intKeyPressed = 0;
      var blnResult = true; // Assume OK
      
      // IE or Netscape?
      if( document.all )
      {
            // Ie
            intKeyPressed = event.keyCode;
      }
      else
      {
            // netscape
            intKeyPressed = e.which
      }
            
      // Did they press space?
      for( intIndex = 0; intIndex < aintInvalidKeys.length; intIndex++ )
      {
            // Is this invalid?
            if( intKeyPressed == aintInvalidKeys[ intIndex ] )
            {
                  // Yes, invalid
                  blnResult = false;
                  break;
            }
      }

      // Return Result
      return blnResult;
}


then in your text box put
onkeypress="return ValidKeys( );"
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 200 total points
ID: 12338616


<INPUT type="text" name="myfield" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/\s/,"");' />
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question