Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java implementation of the HTML5 input email validation?

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!

like image 409
Adam Avatar asked Feb 09 '11 00:02

Adam


People also ask

What is the best Java email address validation method?

commons. validator. EmailValidator class. Seems to be a good starting point.


1 Answers

You can use a regex:

[A-Za-z0-9!#$%&'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(.[A-Za-z0-9-]+)*
like image 95
SLaks Avatar answered Sep 20 '22 19:09

SLaks