1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| package com.ruben.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Pattern;
public class TextValidator {
private static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$"; private static final Pattern PATTERN_REGEX_MOBILE_SIMPLE = Pattern.compile(REGEX_MOBILE_SIMPLE);
private static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$"; private static final Pattern PATTERN_REGEX_MOBILE_EXACT = Pattern.compile(REGEX_MOBILE_EXACT);
private static final String REGEX_TEL = "^(\\d{3,4}-)?\\d{6,8}$"; private static final Pattern PATTERN_REGEX_TEL = Pattern.compile(REGEX_TEL);
private static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"; private static final Pattern PATTERN_REGEX_ID_CARD15 = Pattern.compile(REGEX_ID_CARD15);
private static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$"; private static final Pattern PATTERN_REGEX_ID_CARD18 = Pattern.compile(REGEX_ID_CARD18);
private static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; private static final Pattern PATTERN_REGEX_EMAIL = Pattern.compile(REGEX_EMAIL);
private static final String REGEX_URL = "[a-zA-z]+://[^\\s]*"; private static final Pattern PATTERN_REGEX_URL = Pattern.compile(REGEX_URL);
private static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"; private static final Pattern PATTERN_REGEX_DATE = Pattern.compile(REGEX_DATE);
private static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; private static final Pattern PATTERN_REGEX_IP = Pattern.compile(REGEX_IP);
public static boolean isMobileSimple(CharSequence input) { return isMatch(PATTERN_REGEX_MOBILE_SIMPLE, input); }
public static boolean isMobileExact(CharSequence input) { return isMatch(PATTERN_REGEX_MOBILE_EXACT, input); }
public static boolean isTel(CharSequence input) { return isMatch(PATTERN_REGEX_TEL, input); }
public static boolean isIdCard(CharSequence input) { return isMatch(PATTERN_REGEX_ID_CARD15, input) || isMatch(PATTERN_REGEX_ID_CARD18, input); }
public static boolean isEmail(CharSequence input) { return isMatch(PATTERN_REGEX_EMAIL, input); }
public static boolean isUrl(CharSequence input) { return isMatch(PATTERN_REGEX_URL, input); }
public static boolean isDate(CharSequence input) { return isMatch(PATTERN_REGEX_DATE, input); }
public static boolean isIp(CharSequence input) { return isMatch(PATTERN_REGEX_IP, input); }
public static boolean isMatch(Pattern pattern, CharSequence input) { return StringUtils.isNotEmpty(input) && pattern.matcher(input).matches(); } }
|