验证email address.

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));

  }

}

你可能感兴趣的:(email)