• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

capturing keypresses and checking if alphanumeric

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a problem
Glad to be of assistance.

Sam
 
reply
    Bookmark Topic Watch Topic
  • New Topic