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