Validate email addresses with this comprehensive regular expression pattern that covers most common email formats
^
Start of string anchor[a-zA-Z0-9._%+-]+
Local part: letters, numbers, dots, underscores, percent, plus, and hyphens (one or more)@
Literal @ symbol[a-zA-Z0-9.-]+
Domain name: letters, numbers, dots, and hyphens (one or more)\.
Literal dot (escaped)[a-zA-Z]{2,}
Top-level domain: letters only, 2 or more characters$
End of string anchor// Email validation regex
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
function validateEmail(email) {
return emailRegex.test(email);
}
// Usage examples
console.log(validateEmail("user@example.com")); // true
console.log(validateEmail("test.email+tag@domain.co.uk")); // true
console.log(validateEmail("invalid.email")); // false
import re
# Email validation pattern
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
def validate_email(email):
return bool(re.match(email_pattern, email))
# Usage examples
print(validate_email("user@example.com")) # True
print(validate_email("test.email+tag@domain.co.uk")) # True
print(validate_email("invalid.email")) # False
import java.util.regex.Pattern;
public class EmailValidator {
private static final String EMAIL_PATTERN =
"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
private static final Pattern pattern = Pattern.compile(EMAIL_PATTERN);
public static boolean validateEmail(String email) {
return pattern.matcher(email).matches();
}
public static void main(String[] args) {
System.out.println(validateEmail("user@example.com")); // true
System.out.println(validateEmail("invalid.email")); // false
}
}
Input | Expected | Description |
---|---|---|
user@example.com | ✓ Valid | Standard email format |
test.email+tag@domain.co.uk | ✓ Valid | Email with plus sign and subdomain |
firstname.lastname@company.org | ✓ Valid | Email with dots in local part |
user123@test-domain.com | ✓ Valid | Email with numbers and hyphen |
invalid.email | ✗ Invalid | Missing @ symbol |
@domain.com | ✗ Invalid | Missing local part |
user@ | ✗ Invalid | Missing domain |
user@domain | ✗ Invalid | Missing TLD |
user name@domain.com | ✗ Invalid | Space in local part |
Validate email address format in user registration forms to ensure users enter valid email addresses and avoid issues caused by invalid emails.
// Registration form validation
if (!emailRegex.test(userEmail)) {
showError("Please enter a valid email address");
}
Validate recipient email addresses before sending emails to avoid sending failures due to format errors and improve email delivery rates.
// Email sending validation
const validEmails = recipients.filter(email =>
emailRegex.test(email)
);
Use regular expressions to filter and validate email addresses when importing user data in bulk, ensuring data quality and integrity.
// Data cleaning
const cleanData = rawData.filter(user =>
emailRegex.test(user.email)
);
Validate email address format in request parameters within API interfaces to ensure interface robustness and data validity.
// API parameter validation
if (!emailRegex.test(req.body.email)) {
return res.status(400).json({error: "Invalid email"});
}
Common reasons for email validation failure include: email addresses containing special characters, incorrect domain format, missing @ symbol or top-level domain. Please ensure the email format is: username@domain.suffix
Correct format: user@example.com, test.email+tag@domain.co.uk
Incorrect format: user@domain, @domain.com, user.domain.com
The current regex primarily supports ASCII character domains. If you need to support internationalized domain names (IDN), it's recommended to use specialized email validation libraries or more complex regular expressions.
Note: For production environments, it's recommended to combine server-side validation with email confirmation mechanisms to ensure email address validity.
The local part of an email address (before @) may be case-sensitive, but the domain part is not case-sensitive. In practice, the entire email address is usually converted to lowercase for processing.
// Normalize email address
const normalizedEmail = email.toLowerCase().trim();
No. Regular expressions can only validate whether the email address format is correct, but cannot verify whether the email actually exists. To verify email authenticity, you need to send confirmation emails or use specialized email verification services.