validate string

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.
[161 byte] By [vik1491a] at [2007-11-27 8:02:39]
# 1
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
hunter9000a at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 2

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

bryanoa at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 3
> Message was edited by: > bryanobryano, here's a link on how to use code tags: http://forum.java.sun.com/help.jspa?sec=formatting
petes1234a at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 4
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}$/
Bruno_Grassellia at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 5

> 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}

prometheuzza at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 6

> > 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])

Bruno_Grassellia at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 7

> ...

> 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

prometheuzza at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 8

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

Bruno_Grassellia at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...
# 9
Hi Bruno,remember: Java != JavaScript. ;-) http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#sum
java_knighta at 2007-7-12 19:44:50 > top of Java-index,Java Essentials,New To Java...