package com.community.pocket.util; import android.util.Log; import android.util.Patterns; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.regex.Pattern; /** * 表单校验工具类 */ public class ValidUtil { /** * 校验邮箱 */ public static boolean emailValid(String email) { return Patterns.EMAIL_ADDRESS.matcher(email).matches(); } /** * 校验手机号 */ public static boolean mobilePhoneValid(String mobilePhone) { return Pattern.compile("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$").matcher(mobilePhone).matches(); } /** * 校验密码 */ public static boolean passwordvalid(String password) { return password != null && password.trim().length() > PropertiesUtil.getIntValue("password.length"); } /** * 校验用户名 */ public static boolean usernamevalid(String username) { return username != null && username.trim().length() > PropertiesUtil.getIntValue("username.length"); } /** * 校验验证码 */ public static boolean CAPTCHAValid(String code) { return Pattern.compile("^\\d{6}$").matcher(code).matches(); } /** * 校验预约时间 */ public static boolean timeValid(Object time) { return time != null && Pattern.compile("^\\d{2}:\\d{2}$").matcher(time.toString()).matches(); } /** * 校验备注信息 */ public static boolean notesValid(String notes) { return notes != null && !notes.isEmpty() && notes.length() <= PropertiesUtil.getIntValue("textMultiLine.length"); } /** * 校验标题信息 */ public static boolean titleValid(String title) { return title != null && !title.isEmpty() && title.length() <= PropertiesUtil.getIntValue("title.length"); } /** * 校验日期 */ public static boolean dateValid(String str) { boolean convertSuccess = true; // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写; SimpleDateFormat format = new SimpleDateFormat(PropertiesUtil.getValue("date.pattern"), Locale.getDefault()); try { // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 format.setLenient(false); format.parse(str); } catch (ParseException e) { // e.printStackTrace(); // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 convertSuccess = false; } return convertSuccess; } /** * 比较startDate是否在endDate之前 * * @param startDate * @param endDate * @return */ public static boolean before(String startDate, String endDate) { try { SimpleDateFormat format = new SimpleDateFormat(PropertiesUtil.getValue("date.pattern"), Locale.getDefault()); Date s = format.parse(startDate); Date e = format.parse(endDate); return s != null && e != null && s.before(e); } catch (ParseException e) { e.printStackTrace(); Log.e(ValidUtil.class.getName(), e.toString()); } return false; } /** * 校验信用分 */ public static boolean scoreValid(String score) { if (score != null && Pattern.compile("^\\d+$").matcher(score).matches()) { int value = Integer.parseInt(score); return value > 0 && value <= PropertiesUtil.getIntValue("score.max"); } else { return false; } } }