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

How to determine whether the email is legal in java

Sep 02, 2020 pm 02:47 PM
java Mail

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 of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

How to download the Binance official app Binance Exchange app download link to get How to download the Binance official app Binance Exchange app download link to get Aug 04, 2025 pm 11:21 PM

As the internationally leading blockchain digital asset trading platform, Binance provides users with a safe and convenient trading experience. Its official app integrates multiple core functions such as market viewing, asset management, currency trading and fiat currency trading.

Ouyi Exchange APP Android version v6.132.0 Ouyi APP official website download and installation guide 2025 Ouyi Exchange APP Android version v6.132.0 Ouyi APP official website download and installation guide 2025 Aug 04, 2025 pm 11:18 PM

OKX is a world-renowned comprehensive digital asset service platform, providing users with diversified products and services including spot, contracts, options, etc. With its smooth operation experience and powerful function integration, its official APP has become a common tool for many digital asset users.

Binance official app download latest link Binance exchange app installation portal Binance official app download latest link Binance exchange app installation portal Aug 04, 2025 pm 11:24 PM

Binance is a world-renowned digital asset trading platform, providing users with secure, stable and rich cryptocurrency trading services. Its app is simple to design and powerful, supporting a variety of transaction types and asset management tools.

Binance official app latest official website entrance Binance exchange app download address Binance official app latest official website entrance Binance exchange app download address Aug 04, 2025 pm 11:27 PM

Binance is one of the world's well-known digital asset trading platforms, providing users with safe, stable and convenient cryptocurrency trading services. Through the Binance App, you can view market conditions, buy, sell and asset management anytime, anywhere.

How to compare two strings in Java? How to compare two strings in Java? Aug 04, 2025 am 11:03 AM

Use the .equals() method to compare string content, because == only compare object references rather than content; 1. Use .equals() to compare string values equally; 2. Use .equalsIgnoreCase() to compare case ignoring; 3. Use .compareTo() to compare strings in dictionary order, returning 0, negative or positive numbers; 4. Use .compareToIgnoreCase() to compare case ignoring; 5. Use Objects.equals() or safe call method to process null strings to avoid null pointer exceptions. In short, you should avoid using == for string content comparisons unless it is explicitly necessary to check whether the object is in phase.

How to join an array of strings in Java? How to join an array of strings in Java? Aug 04, 2025 pm 12:55 PM

Using String.join() (Java8) is the easiest recommended method for connecting string arrays, just specify the separator directly; 2. For old versions of Java or when more control is needed, you can use StringBuilder to manually traverse and splice; 3. StringJoiner is suitable for scenarios that require more flexible formats such as prefixes and suffixes; 4. Using Arrays.stream() combined with Collectors.joining() is suitable for filtering or converting the array before joining; To sum up, if Java8 and above is used, the String.join() method should be preferred in most cases, which is concise and easy to read, but for complex logic, it is recommended.

How to use a thread pool in Java? How to use a thread pool in Java? Aug 04, 2025 am 02:54 AM

Using thread pools can improve performance and reduce overhead. The answer is to efficiently handle multitasking by reusing threads; 1. Select the appropriate thread pool type, such as newFixedThreadPool, newCachedThreadPool, newSingleThreadExecutor or newScheduledThreadPool. It is recommended to use ThreadPoolExecutor directly in the production environment for better control; 2. You can use Runnable (no return value) or Callable (there is a return result and get it through Future); 3. The thread pool must be closed correctly, and shutdown() is called first, and then

See all articles