permaculture playing cards
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Data validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Data validation" Watch "Data validation" New topic

Data validation

Sam Stackly
Ranch Hand

Joined: May 04, 2002
Posts: 109
Hi Guys
What is the best way for data validation of numeric input ?
1- in keypressed event
2- after posting data, in controller
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

The easiest way to validate data in a JTextField is to us a PlainDocument, this can be used to prevent certain characters from being entered and also the length. Look at the API at the PlainDocument class, and extend it and you can create a NumericDocument, then extend the JTextField and create a NumericTextField.
Or check isDigit when you do #2. I think #2 is better because it won't fire as much as #1 will.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Sam Stackly
Ranch Hand

Joined: May 04, 2002
Posts: 109
Thanks Mark,
It's great to have you around here,

I agree. Here's the link:
subject: Data validation
It's not a secret anymore!