用户名长度必须在3-15位之间,只能是字母加数字的组合,但不能是纯数字
public static boolean checkUsername(String username) {
int length = username.length();
if (length < 3 && length > 15) {
return false;
}
int numCount = 0;
int letCount = 0;
for (int i = 0; i < length; i++) {
char c = username.charAt(i);
if ((c >= 'A' && c < 'Z') || (c >= 'a' && c <= 'z')) {
letCount++;
} else if (c > '0' && c < '9') {
numCount++;
}
}
if ((numCount + letCount == length) && letCount > 0 && numCount >= 0) {
return true;
}
return false;
}
身份证长度为18位不能以0为开头,前17位必须都是数字,最为一位可以是数字,也可以是大写X或小写x
public static boolean checkIdCard(String idCard) {
int length = idCard.length();
if (length != 18 || idCard.charAt(0) == '0') {
return false;
}
for (int i = 0; i < length - 1; i++) {
if (idCard.charAt(i) < '0' || idCard.charAt(i) > '9') {
return false;
}
}
char c = idCard.charAt(length - 1);
if ((c > '0' && c < '9') || c == 'x' || c == 'X') {
return true;
} else {
return false;
}
}
手机号长度为11位,不能以0为开头,必须都是数字
public static boolean checkPhoneNumber(String phoneNumber) {
int length = phoneNumber.length();
if (length != 11) {
return false;
}
if (phoneNumber.charAt(0) == '0') {
return false;
}
char c;
for (int i = 0; i < length; i++) {
c = phoneNumber.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
验证码长度为5,由4位大写或者小写字母和1位数字组成,同一个字母可重复,数字可以出现在任意位置
public static String generateCaptcha(){
ArrayList<Character> list =new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char)('a'+i));
list.add((char)('A'+i));
}
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = r.nextInt(list.size());
char c = list.get(index);
sb.append(c);
}
int i = r.nextInt(10);
char[] array = sb.append(i).toString().toCharArray();
int randomIndex = r.nextInt(array.length - 1);
char tmp = array[randomIndex];
array[randomIndex] = array[array.length-1];
array[array.length-1]=tmp;
return new String(array);
}
标签:知识点,return,管理系统,int,char,程序员,length,false,array
From: https://www.cnblogs.com/hack2xia/p/16913478.html