Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / Javascript
Article

Keyboard restriction textbox with options and ñ

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
30 Aug 2005 53.1K   348   13   2
Javascript Keyboard Restrictions to apply in a textbox, n tilde ñ

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. 

JavaScript
 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.

JavaScript
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) 

JavaScript
<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:

JavaScript
<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


Written By
Founder Cimar Solutions
Mexico Mexico
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.

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

Comments and Discussions

 
GeneralNice parameter design Pin
Emir AKAYDIN25-Nov-06 8:20
Emir AKAYDIN25-Nov-06 8:20 
Generalsome advancements Pin
Babailiica7-Sep-05 1:42
Babailiica7-Sep-05 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.