Javaの入力チェック処理について
未入力チェック
/** * 未入力チェック(未入力はエラー) * * @param pData チェック対象の文字列 * @return true 正常 false エラー */ public static boolean isInput(String pData){ if ((pData == null) || ("".equals(pData.trim()))) { return false; } return true; }
パスワード解析
protected static final int MIN_PASSWORD_LENGTH = 8; /** * パスワード解析 * * @param password パスワード * @return 強度数 */ public static int analyzePassWord(String password){ int score = 0; int bonus = 0; int excess = 0; int upper = 0; int numbers = 0; int symbols = 0; excess = password.length() - MIN_PASSWORD_LENGTH; // 文字種カウント for (int i=0; i<password.length();i++) { if (password.substring(i, i+1).matches("[A-Z]")) {upper++;continue;} if (password.substring(i, i+1).matches("[0-9]")) {numbers++;continue;} if (password.substring(i, i+1).matches("[\\.\\+\\-\\~@_]")) {symbols++;continue;} } // 組み合わせボーナス if (upper > 0 && numbers > 0 && symbols > 0) { bonus = 25; } else if ((upper > 0 && numbers > 0) || (upper > 0 && symbols > 0) || (numbers > 0 && symbols > 0)) { bonus = 15; } // 小文字のみ if (password.matches("^[a-z]+$")) { bonus = bonus - 15; } // 数字のみ if (password.matches("^[0-9]+$")) { bonus = bonus - 35; } score = (excess * 3) + (upper * 4) + (numbers * 5) + (symbols * 5) + bonus; return score; }
全角チェック
/** * 全角チェック * * @param s 文字列 * @return true 正常 false エラー */ public static boolean isZenStr(String s){ if (!s.matches("^[^ -~。-゚]+$")) { return false; } return true; }
日付文字チェック
/** * 日付文字チェック * * @param s 文字列 * @return true 正常 false エラー */ public static boolean isDateStr(String s){ if (s.trim() == null || s.length() != 8) { return false; } if (!s.matches("^[0-9]+$")) { return false; } Calendar c1 = Calendar.getInstance(); c1.set(Integer.valueOf(s.substring(0, 4)) , Integer.valueOf(s.substring(4, 6)) -1 , Integer.valueOf(s.substring(6, 8))); c1.setLenient(false); try{ Date d = c1.getTime(); }catch(IllegalArgumentException a){ return false; } return true; }