• Advertisement
Sign in to follow this  

[java] Checking characters

This topic is 4364 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement