Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2313973
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2012-01-05 21:03:24

使用Java正则表达式来进行EMail地址的合法性判断
 
主要代码部分:
 
  1. Pattern pattern = Pattern.compile(RegExpression.REG_email);
  2.         Matcher m = pattern.matcher(email);
  3.         if (!m.find()) {
  4.             errors.rejectValue("email", "register.error.email", null, "地址不合法!");
  5.         }
 
将Email的匹配表达式作为一个常量放在类RegException中
  1. package com.demo.spring.util;

  2. public class RegExpression {
  3.     public final static String REG_int = "^\\d*$";
  4.     public final static String REG_float = "^\\d+(\\.\\d+)?$";
  5.     public final static String REG_date = "^[\\d]{4}([-][\\d]{2}){2}$";
  6.     public final static String REG_time = "^[\\d]{4}([-][\\d]{2}){2}([ ][12]{1}[\\d]{1}([:][123456]{1}[\\d]{1}){2}){1}$";
  7.     public final static String REG_datetime = "^[\\d]{4}([-][\\d]{2}){2}([ ][12]{1}[\\d]{1}([:][123456]{1}[\\d]{1}){2}){1}$";
  8.     public final static String REG_email = "^[\\w\\d]+@[\\w\\d]+(\\.[\\w\\d]+)+$";
  9.     public final static String REG_phone = "^(\\d+[-]){0,2}\\d+$";
  10.     public final static String REG_mobile = "^[1]\\d{10}$";
  11.     public final static String REG_postcode = "^\\d{6}$";
  12. //    public final static String REG_url = "/^[\\w]+[:][/]{2}([\\w]+\\.)+([\\w]+[/])+[\\w]+[.][\\w]+$/";

  13.     public final static String REG_url = "^[\\w]+[:][/]{2}([\\w]+\\.)+([\\w]+[/])+[\\w\\.]*$";

  14. };
阅读(1672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~