必須チェック
public static boolean required(String value) {
if (value == null) return false;
//前後の空白を除く
value = value.trim();
//全角の空白を除く
value = value.replaceAll(" ", "");
if (value.length() == 0) return false;
return true;
}
半角数値チェック
UtilityのisMatchメソッドを使用します。
public static boolean isNumber(String str) {
//スペースの時はチェック対象外
if (str.length() == 0) return true;
if (StringUtil.isMatch(str, "^[0-9]+$")) {
return true;
} else {
return false;
}
}
数字チェック
半角数字だけではなく、全角数字、アラビア数字など整数型として解釈出来る文字列はすべてOKとします。
public static boolean isNumeric(String str) {
//スペースの時はチェック対象外
if (str.length() == 0) return true;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i)) == false) {
return false;
}
}
return true;
}
半角チェック
StringクラスのgetBytes()メソッドでバイト数の計算を行い、バイト数=文字列長の場合、OKとします。
public static boolean isHankaku(String value) {
//スペースの時はチェック対象外
if (value.length() == 0) return true;
try {
if ((value.getBytes("Windows-31J").length) != value.length()) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
全角チェック
StringクラスのgetBytes()メソッドでバイト数の計算を行い、バイト数=文字列長 × 2の場合、OKとします。
public static boolean isZenkaku(String value) {
//スペースの時はチェック対象外
if (value.length() == 0) return true;
try {
if ((value.getBytes("Windows-31J").length) != value.length() * 2) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
ひらがなチェック
Character.UnicodeBlock.HIRAGANA文字ブロックと比較します。
Character.UnicodeBlock.HIRAGANA には、「ー−・」が含まれないので、別個に比較します。
UtilityのisMatchメソッドを使用します。
public static boolean isHiragana(String value) {
//スペースの時はチェック対象外
if (value.length() == 0) return true;
for (int i=0; i
カタカナチェック
Character.UnicodeBlock.KATAKANA文字ブロックと比較します。
Character.UnicodeBlock.KATAKANA には、「ー−・゛゜ゝゞ」が含まれないので、別個に比較します。
UtilityのisMatchメソッドを使用します。
public static boolean isKatakana(String value) {
//スペースの時はチェック対象外
if (value.length() == 0) return true;
for (int i=0; i
文字数チェック
最大文字数
public static boolean isMax(String value, int max) {
//スペースの時はチェック対象外
if (value.length() == 0) return true;
//両端の空白(全・半角)を除く
value = value.trim();
if (value.length() > max ) {
return false;
} else {
return true;
}
}
最小文字数
public static boolean isMin(String value, int min) {
//スペースの時はチェック対象外
if (value.length() == 0) return true;
//両端の空白(全・半角)を除く
value = value.trim();
if (value.length() > min ) {
return false;
} else {
return true;
}
}
範囲チェック
public static boolean range(String value, int min, int max){
//スペースの時はチェック対象外
if (value.length() == 0) return true;
//両端の空白(全・半角)を除く
value = value.trim();
int intVal=0;
try {
intVal=Integer.parseInt(value);
} catch (NumberFormatException e) {
return false;
}
if(intVal >= min && intVal <= max){
return true;
}else{
return false;
}
}