?
Solved

javascript  auto remove space from form entry

Posted on 2004-10-18
3
Medium Priority
?
468 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 264 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 800 total points
ID: 12338616


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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

741 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