溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java判斷郵箱是否合法的方法

發(fā)布時間:2020-09-04 10:01:17 來源:億速云 閱讀:949 作者:小新 欄目:編程語言

這篇文章主要介紹java判斷郵箱是否合法的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

java判斷郵箱是否合法的方法:使用正則表達(dá)式判斷,代碼為【boolean b=matcher.matches();if (b) {System.out.println(mail+"有效的郵箱地址!");】。

java判斷郵箱是否合法的方法

java判斷郵箱是否合法的方法:

使用了正則表達(dá)式來進(jìn)行判斷,代碼實(shí)現(xiàn)如下:

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(\\轉(zhuǎn)義符)
        Matcher matcher=pattern.matcher(mail);
        boolean b=matcher.matches();
        if (b) {
            System.out.println(mail+"有效的郵箱地址!");
        }else {
            System.out.println(mail+"的格式錯誤!!");
        }
    }

javascript電子郵箱的合法性驗(yàn)證

  /**
    *
    */
    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++) {//如果保護(hù)不規(guī)則的字符,表示錯誤
       char c = cs.charAt(i);
       if (!Character.isLetter(c) && !Character.isDigit(c)) {
       return false;
       }
     }
   }
   return true;
 }

以上是java判斷郵箱是否合法的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI