Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java regex email validation

 
imran sujoy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd do with the org.apache.commons.validator.EmailValidator class.
 
imran sujoy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic