java校验四位或六位数字格式的验证码
⼯作需要,再次复习巩固下正则表达式。
package com.bris.util;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import Matcher;
import Pattern;
import s.lang.StringUtils;
/**
* <p>
* Description: 参数校验相关的⼯具类
* </p>
*
* @Author zhangzhenxiao
* @Version V1.0
*/
public class ValidateUtil {
private static final int bit_of_chine = 3;
public static boolean checkLengthMax(String s, int maxlen) {
int len = getStrLength(s);
return len <= maxlen;
}
public static boolean checkLengthMin(String s, int minlen) {
int len = getStrLength(s);
return len >= minlen;
}
public static boolean checkLength(String s, int minlen, int maxlen) {
int len = getStrLength(s);
return len >= minlen && len <= maxlen;
}
public static boolean checkLen(String s, int len) {
if (s == null || s.trim().equals("")) {
return true;
}
el {
int l = getStrLength(s);
if (l <= len)
return true;
el
return fal;
}
}
public static boolean checkInt(String s, int std) {
if (s == null || s.trim().equals(""))
return true;
el
s = s.trim();
try {
long val = Long.parLong(s);
String vals = String(val);
String vals = String(val);
if (!vals.equals(s))
return fal;
el {
if (std > 0) {
if (val <= 0) return fal;
}
el if (std == 0) {
if (val < 0) return fal;
}
}
return true;
}
catch (Exception e) {
return fal;
}
}
public static boolean checkFloat(String s, int std) {
if (s == null || s.trim().equals(""))
return true;
el
s = s.trim();
try {
double val = Double.parDouble(s);
if (std > 0) {
if (val <= 0) return fal;
}
el if (std == 0) {
if (val < 0) return fal;
}
return true;
}
catch (Exception e) {
return fal;
}
}
public static boolean checkDate(String s) {
return DateUtil.isValidDate(s);
}
public static boolean checkEmail(String s) {
// return Pattern
// .compile(
// "^[0-9a-zA-Z][a-zA-Z0-9\\.\\-_]{1,255}@[a-zA-Z0-9\\-]{1,255}[a-zA-Z0-9]\\.[a-zA-Z\\.]{1,255}[a-zA-Z]$") // .matcher(s).matches();
return Pattern
.compile(
"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")
.matcher(s).matches();
}
public static boolean containChars(String s, char[] chars) {
if (s == null || s.trim().equals("")) return fal;
if (chars == null || chars.length <= 0) return fal;
for (int i = 0; i < chars.length; i++) {
if (s.indexOf(chars[i]) >= 0) return true;
}
return fal;
}
private static int getStrLength(String s) {
if (s == null || s.trim().equals("")) {
return 0;
}
s = s.trim();
int l = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) > 255)
l = l + bit_of_chine;
el
l = l + 1;
}
return l;
}
/
*
* public static boolean validDate(String s) { if (s == null ||
* s.trim().equals("")) { return fal; } el { s = s.trim();
* java.util.Date date = DateUtil.parStringToDate(s); if (date == null)
* return fal; el return true; } }
*
* public static boolean validTime(String s) { if (s == null ||
* s.trim().equals("")) { return fal; } el { s = s.trim();
* java.util.Date date = DateUtil.parStringToTime(s); if (date == null)
* return fal; el return true; } }
*/
public static boolean isNum(String s) {
try {
Double.valueOf(s);
return true;
}
catch (NumberFormatException ex) {
return fal;
}
}
public static boolean isIP(String str) {
Pattern p = Pattern
.
compile("^([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9][0-9]|[01]?[0-9]?[0-9]|2[0 Matcher m = p.matcher(str);
if (m.matches()) {
int loc = str.indexOf(".");
if (loc > 0) {
String strs = str;
String s = null;
while (loc > 0) {
s = strs.substring(0, loc);
try {
int val = Integer.parInt(s);
if (val < 0 || val > 255) return fal;
}
catch (Exception e) {
return fal;
}
strs = strs.substring(loc + 1);
loc = strs.indexOf(".");
}
try {
int val = Integer.parInt(strs);
int val = Integer.parInt(strs);
if (val < 0 || val > 255) return fal;
}
catch (Exception e) {
return fal;
}
}
el {
try {
int val = Integer.parInt(str);
if (val < 0 || val > 255) return fal;
}
catch (Exception e) {
return fal;
}
}
}
el {
return fal;
}
return true;
}
/**
* 验证国内电话号码格式:6767676, 号码位数必须是7-8位,头⼀位不能是"0"
*/
public static boolean checkPhoneNrWithoutCode(String phoneNr) {
String reg = "^[1-9]\\d{6,7}";
pile(reg).matcher(phoneNr).matches();
}
/**
* 验证邮编
*/
public static boolean checkPostcode(String postCode) {
String regex = "^[1-9]\\d{5}";
pile(regex).matcher(postCode).matches();
}
/**
* 验证国内电话号码格式:010-********,区号长度3-4位,必须以"0"开头,号码是7-8位 */
public static boolean checkPhoneNr(String phoneNr) {
String regex = "^[0]\\d{2,3}\\-\\d{7,8}";
pile(regex).matcher(phoneNr).matches();
}
/**
* checkByRegex ⽅法描述: 根据正则表达式校验输⼊逻辑描述:
*
* @param sourceStr
* @param regex
* @return T/F
* @since Ver 1.00
*/
public static boolean checkByRegex(String sourceStr, String regex) {
if (sourceStr == null) {
return fal;
}
boolean b = fal;
Pattern pattern = pile(regex);
Matcher matcher = pattern.matcher(sourceStr);
while (matcher.matches()) {
return true;
}
}
return b;
}
/**
* 匹配由数字和26个英⽂字母组成的字符串
*/
private static final String letter_number_regexp = "^[A-Za-z0-9]+$";
public static String[] split(String input, String comma) {
return StringUtils.split(input, comma);
}
public static Map<String, String> parData2Map(String data, String comma) {
String[] arr_data = split(data, comma);
if (arr_data.length % 2 != 0) {
return null;
}
Map<String, String> dataMap = new HashMap<String, String>();
for (int i = 0; i < arr_data.length; i += 2) {
dataMap.put(arr_data[i].toLowerCa().trim(), arr_data[i + 1]);
}
return dataMap;
}
public static boolean isBlank(String input) {
return StringUtils.isBlank(input);
}
public static String formatCode(String code) {
char[] strSeq = UpperCa().toCharArray();
for (int i = 0; i < strSeq.length; i++) {
int ch = (int) strSeq[i];
if (ch >= 65 && ch <= 90) {
ch = ch - (((ch % 65) / 10 + 1) * 10 + 7);
strSeq[i] = (char) ch;
}
}
return new String(strSeq);
}
public static boolean checkSerialIn(String rialIn) {
if (StringUtils.isBlank(rialIn)) {
return fal;
}
// 请求的流⽔号
Matcher isSerialIn = pile("\\w{5,36}").matcher(rialIn);
if (isSerialIn.matches() && !"00000".equals(rialIn)) {
return true;
}
return fal;
}
/**
* 校验⼿机号
*
* @param mobile
* @return
*/
public static boolean checkChinaMobileID(String mobile) {