/**
* 验证性别
*/
public String test_sex() {
for (int i = 0; i < 3; i++) {
System.out.println("请输入性别0-->男,1-->女");
Scanner sc = new Scanner(System.in);
String sex = sc.next();
if (sex.length() == 1) {
if (sex.equals("0") || sex.equals("1")) {
return sex;
} else {
System.out.println("输入有误!");
}
} else {
System.out.println("输入有误!");
}
}
return "输入错误次数过多!返回上一层!";
}
/**
* 验证身份证号码
*
* @param id_number
* @return
*/
public String test_Id_Care() {
for (int i = 0; i < 3; i++) {
System.out.println("请输入身份证号码");
Scanner sc = new Scanner(System.in);
String id_number = sc.next();
if (checkCardIdLastNum(id_number) == true) {// 验证身份证长度以及第十八位校验位(要求18位身份证)
if (checkProvinceId(id_number) == true) {// 检查身份证的省份信息是否正确(使用与18/15位身份证)
if (isValidDate(id_number.substring(6, 14)) == true) {// 判断日期是否有效
System.out.println("身份证合法!");
return id_number;
} else {
System.out.println("身份证输入有误!请重新输入!");
}
} else {
System.out.println("身份证输入有误!请重新输入!");
}
} else {
System.out.println("身份证输入有误!请重新输入!");
}
}
return "输入错误次数过多!返回上一层!";
}
/**
* 验证密码
*
* @return
*/
public String test_Password() {
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码(密码必须是字母、数字组合且不少于6个字符不超多16个字符)");
String password = sc.next();
if (password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$")) {
System.out.println("请再次输入密码");
String password0 = sc.next();
if (password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$")) {
if (password.equals(password0)) {
return password;
} else {
System.out.println("密码有误!请重新输入!");
}
} else {
return "密码不合法!或者用户名不合法!(密码必须是字母、数字组合且不少于6个字符不超多16个字符)";
}
} else {
return "密码不合法!或者用户名不合法!(密码必须是字母、数字组合且不少于6个字符不超多16个字符)";
}
}
return "输入错误次数过多!返回上一层!";
}
/**
* 验证电话号码
*
* @return
*/
public static String isMobileNO() {
for (int i = 0; i < 3; i++) {
System.out.println("请输入电话号码");
Scanner sc = new Scanner(System.in);
String mobiles = sc.next();
try {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);
if (m.matches() == true) {
return mobiles;
} else {
System.out.println("电话号码输入有误!请重新输入!");
}
} catch (Exception e) {
return "电话号码输入有误!请重新输入!";
}
}
return "输入错误次数过多!返回上一层!";
}
/**
* 验证中文地址地址
*
* @param realname
* @return
*/
public static String isChina() {
for (int i = 0; i < 3; i++) {
System.out.println("请输入地址:");
Scanner sc = new Scanner(System.in);
String addr = sc.next();
if (Pattern.matches("[\u4e00-\u9fa5|]{2,15}", addr)) {
return addr;
} else {
System.out.println("请输入中文地址!");
}
}
return "输入错误次数过多!返回上一层!";
}
public static boolean isCardId(String cardid) {
return Pattern.matches("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$",
cardid);
}
public static String[] randomArray(int length) {
int index = 0;
String temp = ",";
for (int ii = 0; ii < 1000; ii++) {
for (int i = 0; i < length; i++) {
index = (int) (Math.random() * 10);
temp += (index + "");
}
temp += ",";
}
return temp.split(",");
}
// 身份证前1位每位加权因子
private static int[] power = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 身份证第18位校检码
private static String[] refNumber = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
// 省(直辖市)代码表
private String provinceCode[] = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35",
"36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65",
"71", "81", "82" };
/**
* 检查身份证的省份信息是否正确(使用与18/15位身份证)
*
* @param provinceid
* @return
*/
public boolean checkProvinceId(String provinceid) {
for (String id : provinceCode) {
if (id.equals(provinceid.substring(0, 2))) {
return true;
}
}
return false;
}
/**
* 校验身份证第18位是否正确(只适合18位身份证)
*
* @param cardId
* @return
*/
public static boolean checkCardIdLastNum(String cardId) {
if (cardId.length() != 18) {
return false;
}
char[] tmp = cardId.toCharArray();
int[] cardidArray = new int[tmp.length - 1];
int i = 0;
for (i = 0; i < tmp.length - 1; i++) {
cardidArray[i] = Integer.parseInt(tmp[i] + "");
}
try {
String checkCode = sumPower(cardidArray);
String lastNum = tmp[tmp.length - 1] + "";
if (lastNum.equals("x")) {
lastNum = lastNum.toUpperCase();
}
if (!checkCode.equals(lastNum)) {
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
/**
* 计算身份证的第十八位校验码
*
* @param cardidArray
* @return
*/
public static String sumPower(int[] cardidArray) {
int result = 0;
for (int i = 0; i < power.length; i++) {
result += power[i] * cardidArray[i];
}
return refNumber[(result % 11)];
}
/**
* 判断日期是否有效
*
* @param inDate
* @return
*/
public boolean isValidDate(String inDate) {
if (inDate == null) {
return false;
}
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
if (inDate.trim().length() != dateFormat.toPattern().length()) {
return false;
}
try {
if (dateFormat.parse(inDate).getTime() - date.getTime() > 0
|| dateFormat.parse(inDate).getTime() < -2209017600000L) {
return false;
}
} catch (ParseException e1) {
// TODO Auto-generated catch block
return false;
}
dateFormat.setLenient(false);// 严格的日期匹配
try {
dateFormat.parse(inDate.trim());
} catch (ParseException e) {
return false;
}
return true;
}