File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Textbox-validation! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Textbox-validation!" Watch "Textbox-validation!" New topic


Naina maraikar

Joined: Nov 08, 2000
Posts: 1
dear winners..
fine and praying the same.
i mail this to clearify me how to create two textboxes which were validated as such one box should allow to enter only numbers and other to enter only Strings.
it will be much thankful to get me informed .
mail id :
take care
naina maraikar

Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
I'm not too sure of exactly what you want to test for here but I'll give it a try .
For a "number only" TextField (I'm assuming 'textbox' means this and not a TextArea) simply get the textfield entry using myTextField.getText() and then try to parse it to an int. If you get an NumberFormatException, send the user the appropriate message.For example,

For the "string only" TextField, I have two solutions depending on what you want. If it simply means 'not a number' then do the opposite of the above. Meaning, get the String and if you *can* parse it to an int then it's not a String. However, if you mean absolutly no numbers contained within the entire String (ie. when4we56go is illegal) then you'll have to examine each character at a time. To do this, here are some suggested steps.
1) get the text from the TextField
2) create a Reader like so Reader r = new StringReader( inString);
2) loop though each character and compare it to the range of acii value for numbers. For example,

I haven't tested this code but you get the idea. Hope this helps.

[This message has been edited by Sean MacLean (edited November 10, 2000).]
[This message has been edited by Sean MacLean (edited November 10, 2000).]
I agree. Here's the link:
subject: Textbox-validation!
It's not a secret anymore!