Jaycee
Posts: 26
Nickname: jaycee
Registered: Apr, 2003
|
|
Re: String check
|
Posted: May 29, 2003 12:07 PM
|
|
import java.util.HashMap;
public class PasswordValidator{
private static HashMap _legal_chars;
static{
String[] legalLetters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] legalNumbers = {"0","1","2","3","4","5","6","7","8","9"};
_legal_chars = new HashMap();
String character;
for(int i=legalLetters.length-1; i>=0; i--){
character = legalLetters[i];
_legal_chars.put(character,null);
character = character.toUpperCase();
_legal_chars.put(character,null);
}
for(int i=legalNumbers.length-1; i>=0; i--){
character = legalNumbers[i];
_legal_chars.put(character,null);
}
_legal_chars.put("_",null);
}
public static boolean isPasswordValid(String password){
if(password==null){
throw new IllegalArgumentException("password cannot be null");
}
boolean isValid = true;
char[] chars = password.toCharArray();
for(int i=chars.length-1;i>=0; i--){
if(!_legal_chars.containsKey(String.valueOf(chars[i]))){
isValid = false;
break;
}
}
return isValid;
}
}
|
|