RE Validation in JavaEE

以下是关于RE校验的一些方法的基本功能:

import java.util.regex.Pattern;<br>

/**
* 校验器:利用正则表达式校验邮箱、手机号等
*
*/<br><br>
public class Validator {
<br>/**
* 正则表达式:验证用户名
*/
  //public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";
  public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{2,20}$";
<br>
/**
* 正则表达式:验证密码
*/
  public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
<br>
/**
* 正则表达式:验证手机号
*/
// public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
  public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$";
<br>
/**
* 正则表达式:验证邮箱
*/
  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,}$";
<br>
/**
* 校验用户名
*
* @param username
* @return 校验通过返回true,否则返回false
*/
  public static boolean isUsername(String username) {
    return Pattern.matches(REGEX_USERNAME, username);
    }
<br>
/**
* 校验密码
*
* @param password
* @return 校验通过返回true,否则返回false
*/
  public static boolean isPassword(String password) {
    return Pattern.matches(REGEX_PASSWORD, password);
    }
<br>
/**
* 校验手机号
*
* @param mobile
* @return 校验通过返回true,否则返回false
*/
  public static boolean isMobile(String mobile) {
    return Pattern.matches(REGEX_MOBILE, mobile);
    }
<br>
/**
* 校验邮箱
*
* @param email
* @return 校验通过返回true,否则返回false
*/
  public static boolean isEmail(String email) {
    return Pattern.matches(REGEX_EMAIL, email);
    }
 
  public static void main(String[] args) {
     String username = "fdsdfsdj";
     System.out.println(Validator.isUsername(username));
 
    }
 
}

转载于:https://www.cnblogs.com/taue997/p/11048069.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值