File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Data Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Data Validation" Watch "Data Validation" New topic
Author

Data Validation

Fikile Mokoena
Greenhorn

Joined: Mar 26, 2012
Posts: 2
Hey everyone this is Fix here I've actually just started Java so I'm still a newborn Java baby... I have to create a form that would have some data validated ie The format of the date and ensuring that the ID number only takes 13 numericals and nothing else... How do I go about this? Please help

My form needs to include things like your name, surname, Identity number, Date of Birth, telephone number and email address with the Submit and Cancel button. How do I ensure that a user can't enter any sort of character when for instance I need numerics for an Identity number and they enter characters... How do I do this?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Fikile Mokoena wrote:Hey everyone this is Fix here


Hi, and welcome to the Ranch!

I've actually just started Java so I'm still a newborn Java baby... I have to create a form that would have some data validated ie The format of the date and ensuring that the ID number only takes 13 numericals and nothing else... How do I go about this? Please help


For the date, I'd use java.text.SimpleDateFormat. Try to parse it and catch the exception to indicate invalid format. For the ID, if you're comfortable with regex, you could use String.matches(). Otherwise, just explicitly check the length of the String and then for each character, call the appropriate method from the Character class to see if it is valid.


My form needs to include things like your name, surname, Identity number, Date of Birth, telephone number and email address with the Submit and Cancel button. How do I ensure that a user can't enter any sort of character when for instance I need numerics for an Identity number and they enter characters... How do I do this?


When you say "form", is this going to be a Swing app or a web app? If Swing, then the relevant text widgets might have features to prevent invalid characters from being entered in the first place. I'm not sure, as I don't do GUIs. Otherwise, if those features aren't present, or if you're doing a web app, then you'll need to just validate the field after the fact. (Although I supposed for a web app, JavaScript may have similar validation features for its widgets, but lets not go there yet.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
As a rule, “form” Components do not have data validation built in. I would have thought that full-blown validation is beyond the scope of “a newborn Java baby.”
You can get a few applets which provide data in a particular format. For example, Tödter has a date picker, which will probably only give you dates as return values. That might not be permissible if this is assigned work.
Otherwise things like JTextFields return a String. It is possible to validate those Strings for their form. That is probably a regular grammar, which can be parsed and matched with regular expressions. Again, not an easy topic.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
We need more details. What tools are you to use to create your form? HTML? Swing? The approach to form validation could potentially be very different.
Fikile Mokoena
Greenhorn

Joined: Mar 26, 2012
Posts: 2
Hey guys thank you so much for your responses and to respond to your questions I'm using Swing to create my form
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Validation