Recent Posts

JavaScript function for blacklisting characters in Unicode

JavaScript function for blacklisting characters in Unicode

17:18 18 August in Forms, General, JavaScript

This is a handy little function I wrote today to help me out with a special text field. This field needs to accept number and letters only, no spaces and no other characters.  There is a var for each character I don’t want the field to accept, and there is an array that holds all those vars for the checking loop at the end.

Depending on how this field works out, I may need to restore a couple of characters, like ‘-‘ and ‘_’, for which all I have to do is remove them from the array. My short hand for the characters may be a little vague, but you can find the Unicode for any character by using the Character Mapper in Windows.

function noChars(e)
var keynum;
var keychar;

if(window.event) // IE
	keynum = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
	keynum = e.which;
keychar = String.fromCharCode(keynum);

var space = /u0020/;
var hash = /u0023/;
var exc = /u0021/;
var at = /u0040/;
var dollar = /u0024/;
var perc = /u0025/;
var amp = /u0026/;
var lper = /u0028/;
var rper = /u0029/;
var ast = /u002a/;
var apos = /u0027/;
var lbra = /u007b/;
var pipe = /u007c/;
var rbra = /u007d/;
var cir = /u005e/;
var hm = /u002d/;
var us = /u005f/;
var plus = /u002b/;
var equal = /u003d/;
var sqrl = /u005b/;
var sqrr = /u005d/;
var bakslash = /u005c/;
var forslash = /u002f/;
var dq = /u0022/;
var colon = /u003a/;
var semi = /u003b/;
var ques = /u003f/;
var per = /u002e/;
var com = /u002c/;
var ltag = /u003c/;
var rtag = /u003e/;
var tld = /u007e/;
var acc = /u0060/;

var charChecks = new Array(space,hash,exc,at,dollar,perc,amp,lper, rper,ast,apos,lbra,pipe,rbra,cir,hm,us,plus,equal,sqrl,sqrr,bakslash, forslash,dq,colon,semi,ques,per,com,ltag,rtag,tld,acc);

		var i;
		for (i=0; i < charChecks.length; i++) {
			if (charChecks[i].test(keychar)) {
			return !charChecks[i].test(keychar);



Use it like this:

No Comments

Post A Comment

%d bloggers like this: