Home  >  Article  >  Java  >  How to determine whether the email is legal in java

How to determine whether the email is legal in java

coldplay.xixi
coldplay.xixiOriginal
2020-09-02 14:47:525872browse

java判断邮箱是否合法的方法:可以使用正则表达式来判断。具体代码为【boolean b=matcher.matches();if (b) {System.out.println(mail+"有效的邮箱地址!");】。

How to determine whether the email is legal in java

java判断邮箱是否合法的方法:

【相关学习推荐:java课程

使用了正则表达式来进行判断,代码实现如下:

public class Test {
    public static void main(String[] args) {
        //电子邮件
         String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
         Pattern regex = Pattern.compile(check);
         Matcher matcher = regex.matcher("dffdfdf@qq.com");
         boolean isMatched = matcher.matches();
         System.out.println(isMatched);
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String mail=null;
        System.out.println("请输入E-Mail:");
        mail=scanner.next();
        Pattern pattern=Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");//\w表示a-z,A-Z,0-9(\\转义符)
        Matcher matcher=pattern.matcher(mail);
        boolean b=matcher.matches();
        if (b) {
            System.out.println(mail+"有效的邮箱地址!");
        }else {
            System.out.println(mail+"的格式错误!!");
        }
    }

javascript电子邮箱的合法性验证

  /**
    *
    */
    function isEmail(email)
      {
            var srt=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
              if(srt.test(email))
              {
                  //不合法时
                  return false;
              }
              else
              {
                  //合法时
                return true;
              }
      }
}
public static boolean validateEmail(String email) {
   boolean flag = false;
   int pos = email.indexOf("@");
   if (pos == -1 || pos == 0 || pos == email.length() - 1) {
     return false;
   }
   String[] strings = email.split("@");
   if (strings.length != 2) {// 如果邮箱不是xxx@xxx格式
     return false;
   }
   CharSequence cs = strings[0];
   for (int i = 0; i < cs.length(); i++) {
     char c = cs.charAt(i);
     if (!Character.isLetter(c) && !Character.isDigit(c)) {
       return false;
     }
   }
   pos = strings[1].indexOf(".");// 如果@后面没有.,则是错误的邮箱。
   if (pos == -1 || pos == 0 || pos == email.length() - 1) {
     return false;
   }
   strings = strings[1].split(".");
   for (int j = 0; j < strings.length; j++) {
     cs = strings[j];
     if (cs.length() == 0) {
     return false;
     }
     for (int i = 0; i < cs.length(); i++) {//如果保护不规则的字符,表示错误
       char c = cs.charAt(i);
       if (!Character.isLetter(c) && !Character.isDigit(c)) {
       return false;
       }
     }
   }
   return true;

The above is the detailed content of How to determine whether the email is legal in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn