Sign in to follow this  
Daedalus AI

[java] Checking characters

Recommended Posts

Hey all, I want to check that the characters entered into a JTextField are between 'a' and 'z', so no characters like '/' and '?'. How might I do that ?, as in C/C++ I would have just got the ASCII values of 'a' and 'z', and done a simple "if ( ValueA > ValueB )", etc. Thanx, Daed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Daedalus AI

How might I do that ?, as in C/C++ I would have just got the ASCII values of 'a' and 'z', and done a simple "if ( ValueA > ValueB )", etc.

Thanx,
Daed.


There's no need. this code works just fine.

if (c >= 'a' && c <= 'z')


Where c is a char

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The best option is to ensure that the user can only enter valid characters.

You can do that by extending PlainDocument and overwriting its insertString method. If the input is alright you call super.insertString (and otherwise you can do a getToolkit().beep() for some feedback).

For using this custom PlainDocument thingy you simply hand it over in the JTextField c'tor.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this