Full Stack Development

Simple Email Validator in Java – A Beginner’s Guide

Priyanka Priyanka
Jan 04, 2025 3 Min Read
Java Programming 2026

Simple Email Validator in Java

Learn how to validate email addresses using Regular Expressions (Regex)—a must-have skill for any backend developer.

Why Validate Emails?

Validation ensures that the data entered by a user follows the correct format (e.g., user@domain.com). This prevents database errors and ensures your application can actually send emails to its users.

The Regex Pattern

In Java, we use Regular Expressions to define the "rules" for a valid email. Here is a simple but effective pattern:

^[A-Za-z0-9+_.-]+@(.+)$

The Java Code

We use the java.util.regex package, specifically the Pattern and Matcher classes.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

public class EmailValidator {
// The Regex Pattern
private static final String REGEX = "^[A-Za-z0-9+_.-]+@(.+)$";

<span class="token-keyword">public static void</span> main(String[] args) {
    Scanner sc = <span class="token-keyword">new</span> Scanner(System.in);
    System.out.print(<span class="token-string">"Enter your email: "</span>);
    String email = sc.nextLine();

    Pattern pattern = Pattern.compile(REGEX);
    Matcher matcher = pattern.matcher(email);

    <span class="token-keyword">if</span> (matcher.matches()) {
        System.out.println(<span class="token-string">"✅ Valid Email Address!"</span>);
    } <span class="token-keyword">else</span> {
        System.out.println(<span class="token-string">"❌ Invalid Email Address!"</span>);
    }
}
}

Key Takeaways

  • 1

    Pattern.compile(): Compiles the regex string into a searchable pattern.

  • 2

    matcher.matches(): Returns true if the entire input matches the pattern.

Become a Pro Java Developer

Master Core Java, Regex, and Spring Boot in our 2026 Bootcamp. 100% Placement assistance in top MNCs in Gurgaon, Noida, and Delhi.

Code Breakdown

  • ^ : Start of string
  • [A-Za-z0-9] : Allowed characters
  • + : One or more times
  • @ : Mandatory symbol
  • $ : End of string

Student Perk

Join our community and get free access to 50+ Java interview programs and logic puzzles.

© 2026 4Achievers Academy. Empowering the next generation of Java Engineers.