Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNice parameter design
Emir AKAYDIN
9:20 25 Nov '06  
I really liked giving all allowed keys in a plain string. Also handling of backspace, return, esc and other special keys is very nice. Works quite fine with IE and Firefox. Thanks for these useful functions.
Generalsome advancements
Babailiica
2:42 7 Sep '05  
Well I have added some advancements to ypur script:


function keyRestrict(e, validchars, casesensitives, onceevery, onceoneof) {
onceevery = onceevery ? onceevery : "";
onceoneof = onceoneof ? onceoneof : "";
if (!validchars) return true;
var key='', keychar='', obj='', i = 0;
var key = e.which ? e.which : window.event.keyCode;
var obj = e.target ? e.target : window.event.srcElement;
if (key == null) return true;
keychar = String.fromCharCode(key);
validchars = (validchars + onceevery + onceoneof);
if (!casesensitives) {
keychar = keychar.toLowerCase();
validchars = validchars.toLowerCase();
onceevery = onceevery.toLowerCase();
onceoneof = onceoneof.toLowerCase();
}
for (i=0; i<(onceevery + onceoneof).length; i++) {
if (i < onceevery.length) {
if (obj.value.indexOf(onceevery.charAt(i)) != -1 && keychar == onceevery.charAt(i)) return false;
} else {
if (obj.value.indexOf(onceoneof.charAt(i-onceevery.length)) != -1 && onceoneof.indexOf(keychar) != -1) return false;
}
}
if (validchars.indexOf(keychar) != -1)
return true;
if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
return true;
return false;
}


More parameters are available- case sensitivity is now optional, and added once only and once one of the characters parameters added.

baba...


Last Updated 30 Aug 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010