Click here to Skip to main content
6,628,952 members and growing! (23,613 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Beginner

Keyboard restriction textbox with options and ñ

By Marcelo Lujan [El Bebe.Net ]

Javascript Keyboard Restrictions to apply in a textbox, n tilde ñ
Javascript, Windows, Dev
Posted:30 Aug 2005
Views:24,499
Bookmarked:11 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.54 Rating: 3.00 out of 5
3 votes, 42.9%
1

2
1 vote, 14.3%
3

4
3 votes, 42.9%
5

Introduction

This is a single way to implement a character restriction in a page. Using this Javascript we can to restrict the input characters in a specific way for each control textbox.

Javascript Code

The code is simple, a getkeycode(event) returns the javascript keycode or event.which, depends on the browser. 

 function getKeyCode(e)
{
 if (window.event)
    return window.event.keyCode;
 else if (e)
    return e.which;
 else
    return null;
}

And a keyRestrict(event, validcharacters_string) returns a true or false if the key pressed is a valid character or not.

function keyRestrict(e, validchars) {
 var key='', keychar='';
 key = getKeyCode(e);
 if (key == null) return true;
 keychar = String.fromCharCode(key);
 keychar = keychar.toLowerCase();
 validchars = validchars.toLowerCase();
 if (validchars.indexOf(keychar) != -1)
  return true;
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
  return true;
 return false;
}

Implementation

To use this is simple, just put a function call in the onkeypress event of the textbox

Numeric sample:

<input type="text" name="textfield" onKeyPress="return keyRestrict(event,'1234567890')">

Alfa (not alfanumeric just alfa in this case accepts a SPACE) 

<input type="text" name="textfield" onKeyPress="return keyRestrict(event,'abcdefghijklmnopqrstuvwxyz ')">

� support (spanish keyboards for example).

Here a sample with the � support. because if you type the '�' in the string the script fail in the moment you press the � char.

to have � suport we need to put the next code:

<input type="text" name="textfield" onKeyPress="return keyRestrict(event,'abcdefghijklmnopqrstuvwxyz '+String.fromCharCode(241))">

this is a simple script, but the � support is usefull to me, and i wabt to share the solution

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marcelo Lujan [El Bebe.Net ]


Member
Ing. F. Marcelo Lujan alias El Bebe Dot Net. Hola, yo desarrollo de forma independiente en C#. ASP.NET y Win32 Diseño Macromedia etc. con mas de 10 años de experiencia en informática y soporte a sistemas, así como desarrollo de software y nuevos productos. Por ahora estoy trabajando para el Gobierno del estado de Nuevo Leon en conjunto con Hector Guevara y el Z.

Espero que ayude la informacion que pongo a su disposicion.
I Hope this information that i upload to codeproject helps you.
Atte: Marcelo Lujan

Occupation: Web Developer
Location: Mexico Mexico

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralNice parameter design PinmemberEmir AKAYDIN9:20 25 Nov '06  
Generalsome advancements PinmemberBabailiica2:42 7 Sep '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Aug 2005
Editor:
Copyright 2005 by Marcelo Lujan [El Bebe.Net ]
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project