• Post Reply Bookmark Topic Watch Topic
  • New Topic

capturing keypresses and checking if alphanumeric  RSS feed

 
Noreen Masterson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i am trying to check each character entered and make sure it is only an alpha-numeric char
function keyCheck(e){
var keyChar = String.fromCharCode(e.which);
//here is where u would have to check the key then
}
document.captureEvents(Event.KEYPRESS);
document.onkeypress = keyCheck;
i know in Java u can use char.isLetterorDigit(myString.charAt(index))
but how can this be done in javascript??
thanx
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not really a JSP question, but I would suggest that you look at JavaScript regular expressions, for example:

where my string is the string to match on (in your case the contents of your field), and matched returns true if the regular expression is matched, and false otherwise.
Nb: The regular expression above is probably not correct, so check a reference such as this for more details.
Hope this helps
Sam
 
Noreen Masterson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry if the Q is in the wrong place!!!
anyway- thanx for your help. it was spot on. used the regular expressions in search method
//reg expr /\W/ is a non word
//where a word is made up from [A-Za-z_0-9]
//and /\S\ is non whitespace
if (( keyChar.search(/\W/) != -1) &&(keyChar.search(/\S\) != -1))
alert("char is not alphanumeric or a whitespace")

thanx a million
--Noreen
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem
Glad to be of assistance.

Sam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!