Solved

javascript  auto remove space from form entry

Posted on 2004-10-18
3
460 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call a function within the ASP code 4 19
jquery datable hidden section doesn't submit 2 15
Hide Table in merge 3 32
What if i make webbased alternative for MS Access 5 71
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

828 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