Solved

javascript  auto remove space from form entry

Posted on 2004-10-18
3
451 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
Comment Utility
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
Comment Utility
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
Comment Utility


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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now