aspose file tools*
The moose likes Java in General and the fly likes java regex email validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java regex email validation" Watch "java regex email validation" New topic
Author

java regex email validation

imran sujoy
Greenhorn

Joined: Sep 02, 2008
Posts: 9
I have a requirement of validating email field. Most of the things are done. But I'm stuck at one place. The email allows _, +, . only once in the username part (i.e. before @), once or more, but never consecutively. Also, . can't be immediately before or after @. I couldn't do that checking for consecutive dots or pluses. How to add with \\w a dot or a plus? Can somebody kindly help? Thanks in advance.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
I'd do with the org.apache.commons.validator.EmailValidator class.


Ping & DNS - my free Android networking tools app
imran sujoy
Greenhorn

Joined: Sep 02, 2008
Posts: 9
Thanks for the reply Ulf, but I'm not sure how much EmailValidator class will help when one needs a custom validator (like in my case + allowed, but ... not allowed, though the 2nd one is a perfectly valid email IMO). Anyway, just after posting here I got the solution myself:



The () in place of [] did the trick actually.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
It's generally not a good idea to fail email validation for valid addresses. If an address is good enough for the relevant RFCs, then it should be good enough for you application. Also note that the regexp will fail I18N domain names.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java regex email validation