使用Java正则表达式来进行EMail地址的合法性判断
主要代码部分:
- Pattern pattern = Pattern.compile(RegExpression.REG_email);
- Matcher m = pattern.matcher(email);
- if (!m.find()) {
- errors.rejectValue("email", "register.error.email", null, "地址不合法!");
- }
将Email的匹配表达式作为一个常量放在类RegException中
- package com.demo.spring.util;
- public class RegExpression {
- public final static String REG_int = "^\\d*$";
- public final static String REG_float = "^\\d+(\\.\\d+)?$";
- public final static String REG_date = "^[\\d]{4}([-][\\d]{2}){2}$";
- public final static String REG_time = "^[\\d]{4}([-][\\d]{2}){2}([ ][12]{1}[\\d]{1}([:][123456]{1}[\\d]{1}){2}){1}$";
- public final static String REG_datetime = "^[\\d]{4}([-][\\d]{2}){2}([ ][12]{1}[\\d]{1}([:][123456]{1}[\\d]{1}){2}){1}$";
- public final static String REG_email = "^[\\w\\d]+@[\\w\\d]+(\\.[\\w\\d]+)+$";
- public final static String REG_phone = "^(\\d+[-]){0,2}\\d+$";
- public final static String REG_mobile = "^[1]\\d{10}$";
- public final static String REG_postcode = "^\\d{6}$";
- // public final static String REG_url = "/^[\\w]+[:][/]{2}([\\w]+\\.)+([\\w]+[/])+[\\w]+[.][\\w]+$/";
- public final static String REG_url = "^[\\w]+[:][/]{2}([\\w]+\\.)+([\\w]+[/])+[\\w\\.]*$";
- };
阅读(1675) | 评论(0) | 转发(0) |