I'd like to use the new <input type="email" />
element. I'd like to have Java code that implements the same validation on the server that happens in the browser.
The HTML5 spec defines email addresses in ABNF as:
1*( atext / "." ) "@" ldh-str *( "." ldh-str )
where:
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
<let-dig-hyp> ::= <let-dig> | "-"
<let-dig> ::= <letter> | <digit>
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
<digit> ::= any one of the ten digits 0 through 9
and:
atext = ALPHA / DIGIT / ; Printable US-ASCII
"!" / "#" / ; characters not including
"$" / "%" / ; specials. Used for atoms.
"&" / "'" /
"*" / "+" /
"-" / "/" /
"=" / "?" /
"^" / "_" /
"`" / "{" /
"|" / "}" /
"~"
These are not the same rules as in RFC 5322. How can I test that an address complies with these rules in Java?
Thanks!
commons. validator. EmailValidator class. Seems to be a good starting point.
You can use a regex:
[A-Za-z0-9!#$%&'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(.[A-Za-z0-9-]+)*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With