java check if email is valid code example
Example: email validator java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
public class EmailValidator {
public static void main(String[] args) {
recursion();
}
static void recursion () {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the game of typing a correct email address!");
System.out.println();
System.out.println("As the game says, enter a correct email address.");
System.out.println();
String snow = scanner.nextLine();
String[] sssp = snow.split("@");
if (sssp.length != 2) {
System.out.println();
System.out.println("This is not a correct email address.");
System.out.println();
recursion();
} else {
String regex = "[^a-z0-9._]";
String regex1 = "^\\.|\\.$";
String regex2 = "\s";
String regex3 = "^$";
String regex7 = "\\._|_\\.";
String regex39 = "__";
String regex19 = "\\.\\.";
String regex20 = "^_|_$";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
Pattern pattern3 = Pattern.compile(regex3, Pattern.CASE_INSENSITIVE);
Pattern pattern11 = Pattern.compile(regex7, Pattern.CASE_INSENSITIVE);
Pattern pattern12 = Pattern.compile(regex19, Pattern.CASE_INSENSITIVE);
Pattern pattern13 = Pattern.compile(regex20, Pattern.CASE_INSENSITIVE);
Pattern pattern14 = Pattern.compile(regex39, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sssp[0]);
Matcher matcher1 = pattern1.matcher(sssp[0]);
Matcher matcher2 = pattern2.matcher(sssp[0]);
Matcher matcher3 = pattern3.matcher(sssp[0]);
Matcher matcher11 = pattern11.matcher(sssp[0]);
Matcher matcher12 = pattern12.matcher(sssp[0]);
Matcher matcher13 = pattern13.matcher(sssp[0]);
Matcher matcher14 = pattern14.matcher(sssp[0]);
boolean matchFound = matcher.find();
boolean matchFound1 = matcher1.find();
boolean matchFound2 = matcher2.find();
boolean matchFound3 = matcher3.find();
boolean matchFound10 = matcher11.find();
boolean matchFound11 = matcher12.find();
boolean matchFound12 = matcher13.find();
boolean matchFound13 = matcher14.find();
String hello = sssp[1];
String[] whoop = hello.split("\\.", 2);
if (whoop.length != 2) {
System.out.println();
System.out.println("This is not a correct email address.");
System.out.println();
recursion();
} else {
String regex4 = "[^a-z]";
String regex5 = "^$";
String regex6 = "\s";
Pattern pattern4 = Pattern.compile(regex4, Pattern.CASE_INSENSITIVE);
Pattern pattern5 = Pattern.compile(regex5, Pattern.CASE_INSENSITIVE);
Pattern pattern6 = Pattern.compile(regex6, Pattern.CASE_INSENSITIVE);
Matcher matcher4 = pattern4.matcher(whoop[0]);
Matcher matcher5 = pattern5.matcher(whoop[0]);
Matcher matcher6 = pattern6.matcher(whoop[0]);
boolean matchFound4 = matcher4.find();
boolean matchFound5 = matcher5.find();
boolean matchFound6 = matcher6.find();
Pattern pattern7 = Pattern.compile(regex4, Pattern.CASE_INSENSITIVE);
Pattern pattern8 = Pattern.compile(regex5, Pattern.CASE_INSENSITIVE);
Pattern pattern9 = Pattern.compile(regex6, Pattern.CASE_INSENSITIVE);
Matcher matcher7 = pattern7.matcher(whoop[1]);
Matcher matcher8 = pattern8.matcher(whoop[1]);
Matcher matcher9 = pattern9.matcher(whoop[1]);
boolean matchFound7 = matcher7.find();
boolean matchFound8 = matcher8.find();
boolean matchFound9 = matcher9.find();
System.out.println();
if(matchFound || matchFound1 || matchFound2 || matchFound3 || matchFound4 || matchFound5 || matchFound6 || matchFound7 || matchFound8 || matchFound9 || matchFound10 || matchFound11 || matchFound12 || matchFound13) {
System.out.println("This is not a correct email address.");
System.out.println();
recursion();
} else {
System.out.println("This is a correct email address.");
System.out.println();
System.out.println("Do you still want to play? (say yes to play) ");
System.out.println();
Scanner scanner1 = new Scanner(System.in);
String snow1 = scanner1.nextLine();
if (snow1.equalsIgnoreCase("yes")) {
System.out.println();
recursion();
} else {
System.out.println();
System.out.println("Okay, then.");
scanner.close();
scanner1.close();
}
}
}
}
}
}