Validating Data Inputs

I am accepting some user inputs through TextFields. Before submitting I want to perform certain checks ie. in the numeric field like salary no alphabetic date is entered and say in the name field no digits are entered.

Similarly I also want to chech that the date is also properly entered in the dd-mm-yyyy format.

Thank You.,


qader99Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JodConnect With a Mentor Commented:
To format a date from text use SimpleDateFormat. I have written a small example program below...

import java.util.Date;
import java.text.SimpleDateFormat;

public class tt {
    public static void main(String args[]) {
        Date d =null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            d = sdf.parse("23/12/1999 10:00");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        System.out.println(d.toString());
        try { Thread.sleep(2000); } catch (Exception e) {}
    }
}


SimpleDateFormat allows you to specify the format of the date you wish to accept. Change the format you specify here to adjust this:

new SimpleDateFormat("dd/MM/yyyy HH:mm")

or

new SimpleDateFormat("MM/dd/yyyy HH:mm")

etc...

(Make sure you use MM for month as mm stands for minutes)
0
 
zicaiCommented:
Hi, qader99

To do the data validation, you need to check for each character's ASCII value. If the value falls in [48,57], then it is a digit, else if the value falls in [65,90] or [97,122], it is an alphabetic character. Also, to test whether the char is a digit or not, you can use the method isDigit.

For the date format, what I suggest is you prompt the user with the dd-mm-yyyy format as a label before the text field. Because it's really impossible for the program to tell whether 11-12-1999 is using the format you want. But at the same time, you still can tell that 12-30-1999 is of the wrong format.


Hope this can help you.
Yours sincerely
Zicai - To parse string is tedious...
0
 
comermCommented:
If you find yourself doing this a lot, it can be very convenient to build your own components which do the validations for you. For example, you could build a component based on JTextField called IntegerTextField that only accepts integers, or FloatTextField, etc.

Another approach, which I have used myself, is to take advantage of the fact that Swing text components are document based. You can then build your own document classes that only accept certain types of data, and then you can plug these documents into any of the standard text components...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JodCommented:
Sorry I misread this title - I thought it said Validating Date Inputs, though the above is still valid for date validation anyway.

For the others you need to use the gettext method of the components and retrieve the text the user has typed in, validate it as neccessary and then decide what to do.

Do you need any examples of what I mean.

As comerm says in the long term we have developed components that do the validation - for example we have a number field that only accepts number input in the first place.
0
 
qader99Author Commented:
Dear Jod,
 Examples would definitely help me to understand.

And how to check with ASCII values in java

thanks
0
 
JodCommented:
For dates use...

        Date d;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            d = sdf.parse( myTextField.getText() );
        } catch (Exception e) {
            System.out.println("Incorrect date..." + e);
            // do something about it...
        }
       
this will accept dates such as 10/10/2000.

To check if the text entered is a valid number use...

      int i = 0;
        try {
            i = Integer.parseInt( myTextField.getText() );
        } catch (Exception e) {
            System.out.println("Invalid number entered..." + e);
            // do something about it...
        }

For example, parseInt("473", 10) returns 473 as an int or throws an exception if the number is invalid.


to make sure no numbers are entered into the string you can take two approaches - get a char array and check it or look for substrings which are digits.

Because of Unicode issues I would avoid checking the character codes directly...

Check a Char array...

      int i = 0;
        String s = myTextField.getText();
      char[] c = s.toCharArray();
      boolean foundADigit = false;

      while ( (i < s.length) && (!foundADigit) ) {
            foundADigit = Character.isDigit(c[i]);
            i++;
      }

      if (foundADigit) {
            System.out.println("No numbers allowed in names...");
            //do something...
      }

Make this in to a method and call it for each text field you want to check. Checks each character in the char array.

Or you could use...

      int i = 0;
        String s = myTextField.getText();
      boolean foundADigit = false;

      while ( (i < 10) && (!foundADigit) ) {
            if (s.indexOf("" + i) == -1) {
                  foundADigit = true;
            }
            i++;
      }

      if (foundADigit) {
            System.out.println("No numbers allowed in names...");
            //do something...
      }

This uses the String method indexOf to check for each individual digit - this is done by turning i into a string using "" + i. So if i is 1, it becomes "1".

0
 
qader99Author Commented:
Thank You very much I am able to solve my all problem with the help of above examples.
0
All Courses

From novice to tech pro — start learning today.