zoukankan      html  css  js  c++  java
  • java RE(正则表达式)

    验证姓名,邮箱,手机号,密码
    复制代码

    import java.util.regex.Pattern;
    /**
     * 账户相关属性验证工具
     *
     */
    public class AccountValidatorUtil {
        /**
         * 正则表达式:验证用户名
         */
        public static final String REGEX_USERNAME = "^[a-zA-Z]\w{5,20}$";
     
        /**
         * 正则表达式:验证密码
         */
        public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
     
        /**
         * 正则表达式:验证手机号
         */
        public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(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,}$";
     
       
        /**
         * 校验用户名
         * 
         * @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);
        }
    }
  • 相关阅读:
    CSS+JS实现兼容性很好的无限级下拉菜单
    自动切换的JS菜单
    (2)C#连sqlite
    protobuf编译器
    (67) c# 序列化
    (66) c# async await
    (65)C# 任务
    mbatis 入门
    (64)C# 预处理器指令
    (63)C# 不安全代码unsafe
  • 原文地址:https://www.cnblogs.com/phs007/p/10589137.html
Copyright © 2011-2022 走看看