java验证工具
import java.util.regex.Pattern; /** ?* 校驗器:利用正則表達式校驗郵箱、手機號等 ?* ?* @author liujiduo ?* ?*/ public class Validator { ????/** ?????* 正則表達式:驗證用戶名 ?????*/ ????public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$"; ????/** ?????* 正則表達式:驗證密碼 ?????*/ ????public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$"; ????/** ?????* 正則表達式:驗證手機號 ?????*/ ????public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; ????/** ?????* 正則表達式:驗證郵箱 ?????*/ ????public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; ????/** ?????* 正則表達式:驗證漢字 ?????*/ ????public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$"; ????/** ?????* 正則表達式:驗證身份證 ?????*/ ????public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)"; ????/** ?????* 正則表達式:驗證URL ?????*/ ????public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"; ????/** ?????* 正則表達式:驗證IP地址 ?????*/ ????public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; ????/** ?????* 校驗用戶名 ?????* ?????* @param username ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isUsername(String username) { ????????return Pattern.matches(REGEX_USERNAME, username); ????} ????/** ?????* 校驗密碼 ?????* ?????* @param password ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isPassword(String password) { ????????return Pattern.matches(REGEX_PASSWORD, password); ????} ????/** ?????* 校驗手機號 ?????* ?????* @param mobile ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isMobile(String mobile) { ????????return Pattern.matches(REGEX_MOBILE, mobile); ????} ????/** ?????* 校驗郵箱 ?????* ?????* @param email ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isEmail(String email) { ????????return Pattern.matches(REGEX_EMAIL, email); ????} ????/** ?????* 校驗漢字 ?????* ?????* @param chinese ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isChinese(String chinese) { ????????return Pattern.matches(REGEX_CHINESE, chinese); ????} ????/** ?????* 校驗身份證 ?????* ?????* @param idCard ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isIDCard(String idCard) { ????????return Pattern.matches(REGEX_ID_CARD, idCard); ????} ????/** ?????* 校驗URL ?????* ?????* @param url ?????* @return 校驗通過返回true,否則返回false ?????*/ ????public static boolean isUrl(String url) { ????????return Pattern.matches(REGEX_URL, url); ????} ????/** ?????* 校驗IP地址 ?????* ?????* @param ipAddr ?????* @return ?????*/ ????public static boolean isIPAddr(String ipAddr) { ????????return Pattern.matches(REGEX_IP_ADDR, ipAddr); ????} ????public static void main(String[] args) { ????????String username = "fdsdfsdj"; ????????System.out.println(Validator.isUsername(username)); ????????System.out.println(Validator.isChinese(username)); ????} }
轉載于:https://www.cnblogs.com/qwer520/p/5007202.html
總結
- 上一篇: 2015 11月30日 一周工作计划与执
- 下一篇: Web 四种常见的POST提交数据方式