I have a text and I need to validate it against the following rules:
- The text must have a length of 8 characters
- The text must have a leading letter
- The text must have a trailing letter
- The text must have 6 numbers between the 2 letters
A good example is:L123456X or l123456x
I did it the following way:
public boolean isValidated(String str) {
boolean validated = false;
char a = str.charAt(0);
char z = str.charAt(str.length() - 1);
int i;
if (str.length() == 8 && Character.isLetter(a) && Character.isLetter(z)) {
validated = true;
for ( i = 1; i < str.length() - 1; ++i ) {
if(Character.isLetter(str.charAt(i))) {
validated = false;
break;
}
}
}
return validated;
}
Is there a simpler way?