forked from PriyaGhosal/SkillWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Email Validator
38 lines (30 loc) · 1.26 KB
/
Email Validator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
public static boolean isValidEmail(String email) {
// Regular expression for email validation
String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
// Compile the regular expression
Pattern pattern = Pattern.compile(emailRegex);
// Match the email with the regex pattern
Matcher matcher = pattern.matcher(email);
// Return true if it matches, false otherwise
return matcher.matches();
}
public static void main(String[] args) {
// Create a scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter an email address
System.out.print("Enter an email address to validate: ");
String email = scanner.nextLine();
// Check if the email is valid and print the result
if (isValidEmail(email)) {
System.out.println("The email address \"" + email + "\" is valid.");
} else {
System.out.println("The email address \"" + email + "\" is invalid.");
}
// Close the scanner to prevent resource leaks
scanner.close();
}
}