From 6b7dcdf847e0b7875f97c5b80760d2d6ba36c70c Mon Sep 17 00:00:00 2001 From: Ashish Gupta <30566001+gupta-ashu01@users.noreply.github.com> Date: Sat, 7 Aug 2021 12:34:38 +0530 Subject: [PATCH] Add files via upload --- .../emailvalidation/EmailValidation.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java diff --git a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java new file mode 100644 index 0000000000..97d1b90ff3 --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/emailvalidation/EmailValidation.java @@ -0,0 +1,64 @@ +package com.baeldung.emailvalidation; + +import java.util.regex.Pattern; + +public class EmailValidation { + + private static String regexPattern; + + public static boolean usingSimpleRegex(String emailAddress) { + regexPattern = "^(.+)@(\\S+)$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean usingStrictRegex(String emailAddress) { + regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean usingUnicodeRegex(String emailAddress) { + regexPattern = "^(?=.{1,64}@)[\\p{L}0-9_-]+(\\.[\\p{L}0-9_-]+)*@" + "[^-][\\p{L}0-9-]+(\\.[\\p{L}0-9-]+)*(\\.[\\p{L}]{2,})$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean usingRFC5322Regex(String emailAddress) { + regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean restrictDots(String emailAddress) { + regexPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@" + "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean owaspValidation(String emailAddress) { + regexPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + public static boolean topLevelDomain(String emailAddress) { + regexPattern = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*" + "@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } +} \ No newline at end of file