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

转载于:https://www.cnblogs.com/zsxl/p/4003986.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值