Link to home
Start Free TrialLog in
Avatar of qader99
qader99Flag for India

asked on

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.,


Avatar of zicai
zicai

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...
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
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.
Avatar of qader99

ASKER

Dear Jod,
 Examples would definitely help me to understand.

And how to check with ASCII values in java

thanks
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".

Avatar of qader99

ASKER

Thank You very much I am able to solve my all problem with the help of above examples.