9 Replies Latest reply: Jun 19, 2007 8:17 AM by 807600 RSS

    validate string

    807600
      hi. can someone please assist me. I require to check that a string consists only of characters A...Z, 0..9. Also it may not exceed 8 characters in length.
        • 1. Re: validate string
          807600
          Use String.length() to check the length, and write a regex that matches only letters and numbers.

          http://www.regular-expressions.info/
          http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
          • 2. Re: validate string
            807600
            OR

            1) Check the length of the string by using the length() method, to make sure it does not exceed 8 characters.

            Convert each character to it's ASCII value, then check it again the ASCII table.
            >>>

            for ( int i = 0; i < str.length(); i++ )
            {
            int char = str.charAt( i );

            // the first check sees if it's between 0 and 9 the second if it's from A .. Z
            if ( ( char >= 48 && <= 57 ) || ( char >= 65 && char <= 90 ) )
            {
            char is valid
            }
            else
            {
            char is invalid
            }
            }

            Message was edited by:
            bryano
            • 3. Re: validate string
              807600
              Message was edited by:
              bryano
              bryano, here's a link on how to use code tags:

              http://forum.java.sun.com/help.jspa?sec=formatting
              • 4. Re: validate string
                807600
                You could use the regex special character \w that matches a word(alphanumeric) character (same as [a-zA-Z_0-9]).

                Regex example:
                /^\w{0,8}$/
                • 5. Re: validate string
                  800282
                  You could use the regex special character \w
                  that matches a word(alphanumeric) character (same as
                  [a-zA-Z_0-9]).

                  Regex example:
                  /^\w{0,8}$/
                  Those slashes won't work in Java's regex. Since you're matching the entire String, you don't need to specify the start and end of the String with the ^ and $ signs. And the OP did not mention the character class [a-z], so I'd say this regex should do:
                  [A-Z|0-9]{1,8}
                  • 6. Re: validate string
                    807600
                    You could use the regex special character
                    \w
                    that matches a word(alphanumeric) character (same
                    as
                    [a-zA-Z_0-9]).

                    Regex example:
                    /^\w{0,8}$/
                    Those slashes won't work in Java's regex. Since
                    you're matching the entire String, you don't need to
                    specify the start and end of the String with the ^
                    and $ signs. And the OP did not mention the character
                    class [a-z], so I'd say this regex should do:
                    [A-Z|0-9]{1,8}
                    I never used regex in java.
                    Does \w{1,8} work? (if I need the class [a-z])
                    • 7. Re: validate string
                      800282
                      ...
                      I never used regex in java.
                      Does \w{1,8} work? (if I need the class [a-z])
                      Yep, try running this:
                      System.out.println("aBC46".matches("\\w{1,8}")); // you need to escape the \ in the String
                      • 8. Re: validate string
                        807600
                        Yep, try running this:
                        System.out.println("aBC46".matches("\\w{1,8}"));
                        // you need to escape the \ in the String
                        I tried and it works.
                        public class RegEx {
                             public static void main (String args[]){
                                  System.out.println("aBC46".matches("\\w{1,8}"));
                                  System.out.println("aBC46*".matches("\\w{1,8}"));
                             }
                        }
                        I will read more about java regular expressions, I only know about javascript regular expressions.
                        Thanks for help and sorry for making a question in this topic.
                        • 9. Re: validate string
                          807600
                          Hi Bruno,

                          remember: Java != JavaScript. ;-)

                          http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#sum