12 October, 2010

How to check for alphanumeric characters using javascript

Some times before, i was asked by one of my friend to check for alphanumeric characters using javascript. I did it for him using regular expressions.
Following is the code snippet for checking alphanumeric characters.

var testId = "1123ASD";
if((testId.search(/[^a-zA-Z0-9 ]/g)) {
alert("Matched");
}
else {
alert("Invalid");
}
//you can enter characters like + - *^$%^$, etc to check it

I hope this helps someone like me.

No comments: