Email Validation Pattern
Validate user emails with a conservative regex pattern that accepts common shapes and rejects spaces or missing domains.
Code
import java.util.regex.Pattern;
public class EmailValidator {
// conservative email validation: [email protected], no comments, no quoted local part
static final Pattern EMAIL = Pattern.compile(
"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
static boolean valid(String value) {
return value != null && EMAIL.matcher(value).matches();
}
public static void main(String[] args) {
String[] candidates = {
"[email protected]", "[email protected]",
"[email protected]", "missing-at-sign.example.com",
"no-domain@", "spaces [email protected]"};
for (String candidate : candidates) {
System.out.println(candidate + " -> " + valid(candidate));
}
}
}Inside the snippet
All of the logic lives in the class EmailValidator and the functions valid and main.
The cycling part of this snippet is for (String candidate : candidates).
What to watch for
- No dependency machinery at all — no registry file and no lockfile entry.
Using it
The signature you will call most reads static boolean valid(String value)
Start the program and main runs valid one after another.
The key statements, annotated
return value != null && EMAIL.matcher(value).matches();
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.Email Address ValidatorValidate email syntax, checking the local part, domain labels, and top-level domain against common rules.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- How do you run Email Validation Pattern?
- Just run the file; everything it needs ships with the runtime itself.
Report a problem with this exampleOpens a pre-filled issue on GitHub.