验证email address.

本文介绍了一个用于管理电子邮件地址的Java类,该类包括电子邮件地址的验证方法、更新记录及无效标志等功能。提供了多种构造函数以适应不同场景,并实现了序列化接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class EmailAddress

  implements Serializable

{

  String emailAddress;

  FormattedDate lastUpdate;

  private Boolean invalidIndicator;

 

  public EmailAddress(String newEmailAddress, FormattedDate newLastUpdate)

  {

    this.emailAddress = newEmailAddress;

    this.lastUpdate = newLastUpdate;

  }

 

  public EmailAddress(String newEmailAddress, Date newLastUpdate)

  {

    this(newEmailAddress, new FormattedDate(newLastUpdate));

  }

 

  public EmailAddress(String newEmailAddress)

  {

    this.emailAddress = newEmailAddress;

  }

 

  public String getEmailAddress()

  {

    return this.emailAddress;

  }

 

  public FormattedDate getLastUpdate()

  {

    return this.lastUpdate;

  }

 

  public String getLastUpdateFormatted()

  {

    return this.lastUpdate.getDateFormatted();

  }

 

  public Boolean getInvalidIndicator() {

    return this.invalidIndicator;

  }

 

  public void setInvalidIndicator(Boolean invalidIndicator) {

    this.invalidIndicator = invalidIndicator;

  }

 

  public static boolean validate(String email)

  {

    if (email != null) {

      StringBuffer sb = new StringBuffer(email.trim());

      if (sb.length() > 0) {

        int idx = email.indexOf('@');

        if (idx < 0) {

          return false;

        }

      }

    }

    return true;

  }

 

  public static boolean validate2(String email)

  {

    boolean flag = true;

    if (email != null) {

      int idx = email.indexOf("@");

      if ((idx == -1) || (email.indexOf("@", idx + 1) != -1))

        flag = false;

      else if (email.indexOf(".") == -1)

        flag = false;

      else if (email.indexOf(",") != -1)

        flag = false;

      else if (email.indexOf(";") != -1)

        flag = false;

      else if (email.length() < 5)

        flag = false;

      else if (email.indexOf(" ") != -1) {

        flag = false;

      }

      else if (!validCharAfterAtSign(email))

        flag = false;

      else if (!validLastChar(email))

        flag = false;

      else if (idx == 0)

        flag = false;

    }

    else

    {

      flag = false;

    }

    return flag;

  }

 

  public String toString()

  {

    return new StringBuilder().append("EmailAddress: emailAddress=(").append(this.emailAddress == null ? "null" : this.emailAddress).append(") ").append("lastUpdate=(").append(this.lastUpdate == null ? "null" : getLastUpdateFormatted()).append(") ").append("invalidIndicator=(").append(this.invalidIndicator).append(")").toString();

  }

 

  private static boolean validCharAfterAtSign(String str)

  {

    int idx = str.indexOf("@");

    for (int i = idx + 1; i < str.length(); i++) {

      int charInt = str.charAt(i);

 

      if ((charInt != 45) && (charInt != 46) && ((charInt < 48) || (charInt > 57)) && ((charInt < 65) || (charInt > 90)) && ((charInt < 97) || (charInt > 122)))

      {

        return false;

      }

    }

    return true;

  }

 

  private static boolean validLastChar(String str) {

    int charInt = str.charAt(str.length() - 1);

 

    return ((charInt >= 48) && (charInt <= 57)) || ((charInt >= 65) && (charInt <= 90)) || ((charInt >= 97) && (charInt <= 122));

  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值