Email validation using Java Regex

📧 Email Validation Using Java Regular Expressions (Regex)

Topic: Java Regex Pattern for Email Validation


🧩 Overview

In this post, we’ll explore how to validate email addresses in Java using Regular Expressions (Regex).
The regex pattern enforces strict rules for valid email structures — ensuring only properly formatted email IDs are accepted.


🧠 Regex Pattern

^[A-Z0-9._]+@[A-Z0-9-]+\.[A-Z]{2,6}$

🔍 Pattern Breakdown

SymbolMeaningDescription
^Start of the patternEnsures the match begins from the start of the string
[A-Z0-9._]+First partThe local part (before @) must contain uppercase letters (A-Z), digits (0-9), and . or _
@SeparatorSeparates the username and domain parts
[A-Z0-9-]+Second partThe domain name (after @) must contain letters, digits, or hyphens (-)
\.[A-Z]{2,6}Third partThe domain extension must begin with a . followed by 2 to 6 letters (A-Z)
$End of the patternEnsures the match ends at the end of the string

🧩 In simple terms:

The email must look like USERNAME@DOMAIN.TLD,
where:

  • Username = letters, numbers, dots, or underscores

  • Domain = letters or numbers

  • TLD (Top-Level Domain) = 2–6 letters only (e.g., .com, .org, .co.in)


🧰 Java Implementation

Here’s the complete Java example to validate emails using this regex pattern.

package com.vinod.test; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author vinod.kumaran * * Simple email validator using Java Regular Expressions. * * Pattern: ^[A-Z0-9._]+@[A-Z0-9-]+\.[A-Z]{2,6}$ * * ^ = Start of pattern * [A-Z0-9._] = First part: allowed characters before '@' * @[A-Z0-9-] = Second part: allowed characters after '@' * \.[A-Z]{2,6} = Third part: domain extension with 2–6 letters * $ = End of pattern */ public class EmailValidator { // Compile the regex pattern (case-insensitive) public static final Pattern EMAIL_REGEX = Pattern.compile("^[A-Z0-9._]+@[A-Z0-9-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE); public static void main(String[] args) { // Valid email System.out.println(validate("kkvinod@pretechsol.com")); // Invalid email: first part contains '%' System.out.println(validate("kkvin%od@pretechsol.com")); // Invalid email: second part contains '_' System.out.println(validate("kkvinod@prete_chsol.com")); // Invalid email: domain extension > 6 characters System.out.println(validate("kkvinod@pretechsol.comcomcom")); } // Validation method public static boolean validate(String emailAddress) { Matcher matcher = EMAIL_REGEX.matcher(emailAddress); return matcher.find(); } }

🧪 Sample Output

true false false false

✅ Explanation of Results

EmailExpected ResultReason
kkvinod@pretechsol.com✅ trueValid format
kkvin%od@pretechsol.com❌ false% not allowed before @
kkvinod@prete_chsol.com❌ false_ not allowed in domain name
kkvinod@pretechsol.comcomcom❌ falseTLD (extension) exceeds 6 letters 


No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts