473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

disabling keys: limit user to alphabet and numbers

59
when user press can key than the code below gets the key user just pressed. the problem is that i want to only limit user to press alphabet and numbers. so i want to disable weird char ex left, right, up, @,$,!,%..... as you can see this will take up alot of if statments. is there better way to do this?


Expand|Select|Wrap|Line Numbers
  1. if(keys == KeyEvent.VK_SHIFT || keys == KeyEvent.VK_CAPS_LOCK || keys ==KeyEvent.VK_LEFT ||
  2.    keys ==KeyEvent.VK_RIGHT || keys ==KeyEvent.VK_DOWN || keys ==KeyEvent.VK_UP   .....)  //more button to disable
  3. {
  4. }
  5. else{  //store the char in array            
  6.     username[z] = e.getKeyChar();
  7.     z++;
  8. }
  9.  
May 3 '13 #1
4 2017
Rabbit
12,516 Expert Mod 8TB
Rather than blacklisting the keys, whitelist the ranges that are allowed.
May 3 '13 #2
game2d
59
whats whitlist the range? is there tutorial online how to do this?
May 3 '13 #3
Nepomuk
3,112 Expert 2GB
I don't know of a tutorial, but what Rabbit means is the following:
As you can see in the KeyEvent API docs, all of these events are basically integers and can be compared as such. So you can check which keys should be allowed (hence a whitelist) by something like this:
Expand|Select|Wrap|Line Numbers
  1. if( (keys >= KeyEvent.VK_0 && keys <= KeyEvent.VK_9)
  2.  || (keys >= KeyEvent.VK_A && keys <= KeyEvent.VK_Z)) {
  3.   // do stuff
  4. } else {
  5.   // Tell the user he pressed a deactivated key. Or don't. Your choice.
  6. }
May 4 '13 #4
game2d
59
ah i see. i didnt knew we could do this. thanks alot guys
May 4 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Niels Borg | last post by:
Hi, I am building an app, where I would like to disable the standard windows function keys. I.E. F4 which expands a a selected drop-down list. Can anyone help me on that? Many thanks Niels...
5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
5
by: glenn.owens | last post by:
In the process of doing some routine monitoring/clean-up we've discovered that several (many?) users are apparently set to access our SQL Server 2000 database instances via the Named Pipes...
3
by: Armando | last post by:
Apologies if this has been covered recently - I lurked and saw nothing. I have an application that does ticketing at a three-screen movie theatre, and I'm using the function keys F1 thru F12...
3
by: PB | last post by:
What is the rationalle for disabling JavaScript. AFAIK, the primary reason is for "security purposes" - but what specific kind of threats does the protect against? AND - is the disabling of...
12
by: Nalaka | last post by:
Hi, I suddenly started getting a lot of errors from html validation (some CSS) so I followed the following instructions to disable it. If you'd rather not have these types of HTML validation...
1
by: JJ | last post by:
I have a createuserwizard control on my web form. When the user registration fields are filled out correctly, and the user then presses the return key instead of clicking the 'create user' button,...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
2
by: Duckof85 | last post by:
How do I restore a programs default funtion keys? After installing the software for the new keyboard, the only functions I can preform are the ones from the software. I would like to be able to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.