Created
September 13, 2023 09:21
-
-
Save jsadeli/f00a6701df408ec59ba9e3f8c1ddb43a to your computer and use it in GitHub Desktop.
email address format validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.Predicate | |
import java.util.regex.Pattern | |
import org.springframework.stereotype.Component | |
/** | |
* Email address format validator. | |
* | |
* @author Jeffrey Sadeli | |
* @since 2023-05-16 | |
*/ | |
@Component | |
class EmailAddressFormatValidator : Predicate<String> { | |
/** | |
* Validates email address format. | |
* | |
* @param emailAddress the input argument | |
* @return `true` if is a valid email address format, otherwise `false`. | |
* @author Jeffrey Sadeli | |
* @see <a href="https://datatracker.ietf.org/doc/html/rfc5322#section-3.4">RFC 5322</a> | |
* @see <a href="https://owasp.org/www-community/OWASP_Validation_Regex_Repository">OWASP</a> | |
* @since 2023-05-16 | |
*/ | |
override fun test(emailAddress: String): Boolean { | |
// slight modification to OWASP regex: placing a 64-characters limit in the local part | |
val regex: String = | |
"""^(?=.{1,64}@)[a-zA-Z\d_+&*-]+(?:\.[a-zA-Z\d_+&*-]+)*""" + | |
"@" + | |
"""(?:[a-zA-Z\d-]+\.)+[a-zA-Z]{2,7}${'$'}""" | |
return Pattern.compile(regex).matcher(emailAddress).matches() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment