EmailValidator.rar

  • PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 7
    下载次数
  • 2005-09-01 11:05
    上传日期
进行email地址有效性检查的代码,利用正则表达式进行处理
EmailValidator.rar
  • www.pudn.com.txt
    218B
  • EmailValidator.java
    5.8KB
内容介绍
/* * Created on Apr 27, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package com.moonlight.www.util; import org.apache.oro.text.perl.Perl5Util; /** * @author eric * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public class EmailValidator { private static final String SPECIAL_CHARS = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]"; private static final String QUOTED_USER = "(\"[^\"]*\")"; private static final String ATOM = VALID_CHARS + '+'; private static final String WORD = "(" + ATOM + "|" + QUOTED_USER + ")"; // Each pattern must be surrounded by / private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/"; private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/"; private static final String IP_DOMAIN_PATTERN = "/^\\[(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\\]$/"; private static final String USER_PATTERN = "/^\\s*" + WORD + "(\\." + WORD + ")*\\s*$/"; private static final String DOMAIN_PATTERN = "/^\\s*" + ATOM + "(\\." + ATOM + ")*\\s*$/"; private static final String ATOM_PATTERN = "/(" + ATOM + ")/"; private static final EmailValidator instance = new EmailValidator(); public static EmailValidator getInstance() { return instance; } protected EmailValidator() { super(); } /** * <p> * Checks if a field has a valid e-mail address. * </p> * * @param email * The value validation is being performed on. A * <code>null</code> value is considered invalid. */ public boolean isValid(String email) { if (email == null) { return false; } Perl5Util matchAsciiPat = new Perl5Util(); if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) { return false; } email = stripComments(email); // Check the whole email address structure Perl5Util emailMatcher = new Perl5Util(); if (!emailMatcher.match(EMAIL_PATTERN, email)) { return false; } if (email.endsWith(".")) { return false; } if (!isValidUser(emailMatcher.group(1))) { return false; } if (!isValidDomain(emailMatcher.group(2))) { return false; } return true; } /** * Returns true if the domain component of an email address is valid. * * @param domain * being validatied. */ protected boolean isValidDomain(String domain) { boolean symbolic = false; Perl5Util ipAddressMatcher = new Perl5Util(); if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) { if (!isValidIpAddress(ipAddressMatcher)) { return false; } else { return true; } } else { // Domain is symbolic name Perl5Util domainMatcher = new Perl5Util(); symbolic = domainMatcher.match(DOMAIN_PATTERN, domain); } if (symbolic) { if (!isValidSymbolicDomain(domain)) { return false; } } else { return false; } return true; } /** * Returns true if the user component of an email address is valid. * * @param user * being validated */ protected boolean isValidUser(String user) { Perl5Util userMatcher = new Perl5Util(); return userMatcher.match(USER_PATTERN, user); } /** * Validates an IP address. Returns true if valid. * * @param ipAddressMatcher * Pattren matcher */ protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) { for (int i = 1; i <= 4; i++) { String ipSegment = ipAddressMatcher.group(i); if (ipSegment == null || ipSegment.length() <= 0) { return false; } int iIpSegment = 0; try { iIpSegment = Integer.parseInt(ipSegment); } catch (NumberFormatException e) { return false; } if (iIpSegment > 255) { return false; } } return true; } /** * Validates a symbolic domain name. Returns true if it's valid. * * @param domain * symbolic domain name */ protected boolean isValidSymbolicDomain(String domain) { String[] domainSegment = new String[10]; boolean match = true; int i = 0; Perl5Util atomMatcher = new Perl5Util(); while (match) { match = atomMatcher.match(ATOM_PATTERN, domain); if (match) { domainSegment[i] = atomMatcher.group(1); int l = domainSegment[i].length() + 1; domain = (l >= domain.length()) ? "" : domain.substring(l); i++; } } int len = i; if (domainSegment[len - 1].length() < 2 || domainSegment[len - 1].length() > 4) { return false; } // Make sure there's a host name preceding the domain. if (len < 2) { return false; } return true; } /** * Recursively remove comments, and replace with a single space. The simpler * regexps in the Email Addressing FAQ are imperfect - they will miss * escaped chars in atoms, for example. Derived From Mail::RFC822::Address */ protected String stripComments(String emailStr) { String input = emailStr; String result = emailStr; String commentPat = "s/^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/$1 /osx"; Perl5Util commentMatcher = new Perl5Util(); result = commentMatcher.substitute(commentPat, input); // This really needs to be =~ or Perl5Matcher comparison while (!result.equals(input)) { input = result; result = commentMatcher.substitute(commentPat, input); } return result; } public static void main(String[] args) { EmailValidator email = new EmailValidator(); if(email.isValid("a@a.cn")) { System.out.println("the email addr is good"); } else { System.out.println("the email addr is not good"); } } }
评论
    相关推荐
    • html-validator
      #Email & HTML Validator 这是 MRM-BA 的内部项目,用于验证前端团队制作的电子邮件。 ###作者和贡献者 胡安·巴勃罗·法拉利 : 巴勃罗·加拉诺 : 巴勃罗·奥尔蒂斯 [(pablo19)] ( ): ##Requirements 要运行和...
    • validator
      规格验​​证器 该软件包的主要原理是确保开发人员一次又一次地定义服务... yarn add @spec-validator/validator REST API原语,例如与节点的http服务器的绑定以及HTTP协议的类型安全规范。 yarn add @spec-valida
    • Struts1.3 Validator 验证框架校验Email 例子
      Struts1.3 与1.2向比验证框架部分还是有所区别的,该例子通过一个简单的Email的校验,演示如何通过Struts Validate验证框架进行在页面中生成JavaScripte脚本,进行客户端校验。下完后记得评分,资源分就会回来,还能...
    • bootstrapvalidator
      表单几条需要使用的插件,使用这个比较方便表单几条需要使用的插件,使用这个比较方便
    • email Validator-开源
      这是一个用于验证电子邮件地址的 PHP 类。 它可用于验证语法并检查电子邮件服务器是否存在及其可用。
    • Laravel开发-laravel-email-validator
      Laravel开发-laravel-email-validator 提供外观以帮助验证和验证电子邮件地址
    • validator:
      包验证器 包验证器基于标签对结构和单个字段实现值验证。 它具有以下独特功能: 通过使用验证标签或自定义验证程序进行... go get github.com/go-playground/validator/v10 然后将验证程序包导入到您自己的代码中。
    • React-Validator
      email: "required|email", lastName: "required_if:name,naomi", password: "required|min:8|max:25|alpha_num_punct|confirmed:password_confirmation", password_confirmation: "required
    • Laravel开发-email-validator
      Laravel开发-email-validator 验证Laravel电子邮件的验证规则
    • Super Email Validator V43.zip
      Email validator free