» in my experience...

» home | about | contact | résumé
» archives | donate | rss syndication

»
»
NASA's Ion engine


Communiblog Communiblog expressed as RSS 2.0
Here at IMX
Memes R' Us
freetheaudio2.jpg
SuperNova 1987A from 1994 to 2003
GarageBand

Detecting alphanumeric characters in JavaScript.
[ Posted by Dan on January 30, 2004 | 4 Comments ]

It's 2004 already and just yesterday I wrote a JavaScript function to tell if a character passed into the function is an alpha-numeric character. One would think that after writing JavaScript for several years that the need for this check would have come up already (and furthermore that there would be a built in method in the language, but that's not the case).

I approached the problem by using ASCII character codes. If you have BBEdit, there's a pallette that shows all of those code next to their characters, so I used that to write an annoyingly long if statement with four "or" statements and two "and" statements imbedded in an or statement. Annoying, but effective. Here it is...

function alphaNumericCheck(theChar) {

	if ((theChar < 48) || (theChar > 122) || 
	   ((theChar > 57) && (theChar < 65)) || 
	   ((theChar > 90) && (theChar < 97))   ) {
		return false;
	} else {
		return true;
	}
}
To call the function, you would say something like...
<input type="button" name="foo" value="my button"
    onclick="alphaNumericCheck(this.value.charCodeAt(0))">
The part that says charCodeAt(0) will return the ASCII number for the character at the zeroth position in the string (the first character). The function will take that character and return "false" if it's not a number, a capital letter or a lowercase letter, and "true" if it is alphanumeric. Try it.

 

Why not use Regex? Not sure if this works, but it should be close:

function alphaNumericCheck(){
var regex=/[0-9A-Za-z]*/;
if(regex.test(document.formName.inputName.value)){
return true;
} else {
return false;
}
}

-Posted by Eric on February 2, 2004 01:10 PM

Excellent question! I like your solution WAY better but I tend to shy away from regexing in JavaScript for some reason (my office mate and I have both had problems in the past).

The regex object is a JavaScript 1.2 feature and should be acceptable for use in my env (which is massively heterogeneous, but mostly is IE5 and above, Mozilla 1.4 and above and Safari 1.0.1 and above).

-Posted by Dan on February 2, 2004 02:03 PM

You may have already seen this, but if not, it can be a wonderful tool.
Qforms

Javascript web API... basically lets you do a ton of javascript stuff, particularly form validation.

-Posted by JC on February 2, 2004 03:49 PM

That's allright about java script.

But to do the same function in a Java class, what should i import and then which class should i make an object of?

-Posted by aj on April 11, 2004 03:36 AM




Comment posting has been turned off because I don't have enough time and will to deal with the constant comment spamming. I'm very sorry and will fix this sometime soon (soon = before 2004 ends).

MovableType AmphetaDesk
NetNewsWire BlogTree Subscribe with Bloglines RSS Feed
Copyright © 2001 - 2003 by Daniel Kapusta