Solved

javascript  auto remove space from form entry

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular - code works but need to make a change 14 32
javascript comparing two dates 3 19
How to convert from xls to xlsx using java 7 40
Button and js nou working 3 8
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 …
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 …
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…

809 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