3 Replies Latest reply on Jun 24, 2009 2:05 PM by 807588

    Printable ASCII character Check

    807588
      Hi, friends

      I am implementing password validation for special characters. How can I check whether the character is printable ASCII character or not?

      Is there any way (API method of Java) to identify the same?
        • 1. Re: Printable ASCII character Check
          807588
          if (password.matches("[ -~]+"))
          Note the space before the '-' .
          • 2. Re: Printable ASCII character Check
            807588
            Well. Thanks for the reply,
            I have implemented the validation. Password can have alphanumeric values and some defined special characters.

            public class DemoValidation {

                 public static void main(String args[]) {
                      DemoValidation d = new DemoValidation();
                      String name = "wel$come#123";
                      System.out.println(isValidPassword(name));
                 }

            public static boolean isValidSpecialCharacter(char c)
            {
            boolean isValidChar = false;
            char[] domainValues = {     '!', '@', '#', '$', '%',
                                                    '^', '&', '*', '(', ')',
                                                    '_', '-', '+', '=', '|',
                                                    '\\', '{', '[', '}', ']',
                                                    ':', ';', '"', '\'', '<',
                                                    ',', '>', '.', '?', '/'
                                               };
            for (int i= 0; i < domainValues.length; i++) {
                 if (c == domainValues) {
                      isValidChar = true;
                      break;
                 }
            }
            return isValidChar;
            }
                 
                 /**
            * @param inputStr
            * @return boolean: true of valid string
            * @desc Check for special characters string
            */
                 public static boolean isValidPassword(String inputStr)
            {
            boolean isValidStr = true;
            for (int i= 0; i < inputStr.length(); i++) {
                           char ch= inputStr.charAt(i);
                           if (!isAlphaNumeric(ch+"")) { // Special Character
                                // Domain Value Check
                                if (!(isValidSpecialCharacter(ch))) {
                                     isValidStr = false;
                           break;
                                }
                           }
                      }
            return isValidStr;
            }
                 
                 public static boolean isAlphaNumeric(String str) {
                      for (int i= 0; i < str.length(); i++) {
                           char ch= str.charAt(i);
                           if (!(Character.isLetterOrDigit(ch)))
                                return false;
                      }
                      return true;
                 }

            }
            • 3. Re: Printable ASCII character Check
              807588
              Jaykishan wrote:
              Well. Thanks for the reply,
              I have implemented the validation. Password can have alphanumeric values and some defined special characters.
              That is not what you asked for in your original post.

              All that code can be replaced by one matches() primed with a regular expression.