0

To detect wrong email address such as "hi@myio..io"

  VALID_EMAIL_REGEX =      /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  VALID_EMAIL_REGEX_FULL = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

First one fails. Second suceeds. I don't understand how does this part make it different? (\.[a-z]+)*\.[a-z]

Thank you!

BufBills
  • 8,005
  • 12
  • 48
  • 90
  • Here's a valid one from Wiki: `a.little.lengthy.but.fine@dept.example.com`. Your second regex allows repeating `.xxx` after `@`; the first (after the error noted by @Stepan is corrected) does not. – Cary Swoveland Feb 18 '14 at 03:48

5 Answers5

1

The better answer is that using a regular expression for matching email addresses is a bad idea. For one, all valid addresses are not active. hd1@jsc.d8u.us is me, hd2@jsc.d8u.us is a valid email address by every RFC in existence, but it's not an active email account.

If you want to do email address validation, you could do worse than to set up a web service that does nothing more than take a string, use JavaMail's address parsing (InternetAddress.parse()), which throws an exception if the parse fails and returns the address if it succeeds. Sample code below:

public class ValidationServlet extends HttpServlet {
   protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String candid8Address = request.getParameter("email");
       try {
           InternetAddress.parse(candid8Address);
           response.setStatus(HttpServletResponse.SC_OK);
       } catch (AddressException e) {
           response.setStatus(HttpServletResponse.SC_NOT_FORBIDDEN);
       }
   }
}

Let me know if you need further assistance...

hd1
  • 33,938
  • 5
  • 80
  • 91
  • While I agree that a regex is not the correct way, as it has been documented many times over and over, especially on SO, for instance [here](http://stackoverflow.com/a/201378/846250), using a Java web service from Ruby sounds like a really awful idea. There are tons of tools available on Ruby for that. – To마SE Feb 18 '14 at 05:16
0

In the first one @[a-z\d\-.] has . which matches with any character including an .. It should be removed so the domain will only match a alphanumeric character. It should be:

/\A[\w+\-.]+@[a-z\d\-]+\.[a-z]+\z/i
Stepan Grigoryan
  • 3,062
  • 1
  • 17
  • 6
0

Try this :

/^([\w.%+-]+)@([\w-]+.)+([\w]{2,})$/i

To validate the email address

VDN
  • 498
  • 3
  • 15
0

Try the following:--

/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
Alok Anand
  • 3,346
  • 1
  • 20
  • 17
  • regex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i 1.9.2p320 :002 > email = "hi@myio..io" => "hi@myio..io" 1.9.2p320 :003 > email.match(regex) => nil – Alok Anand Feb 18 '14 at 09:22
0

I once found this one:

# RFC822 Email Address Regexp
# ---------------------------
#
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
# http://creativecommons.org/licenses/by-sa/2.5/
#
# (see: http://tfletcher.com/lib/rfc822.rb)
RFC822 = begin
  qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
  dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
  atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
    '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
  quoted_pair = '\\x5c[\\x00-\\x7f]'
  domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
  quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
  domain_ref = atom
  sub_domain = "(?:#{domain_ref}|#{domain_literal})"
  word = "(?:#{atom}|#{quoted_string})"
  domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
  local_part = "#{word}(?:\\x2e#{word})*"

  # The following line was needed to change for ruby 1.9
  # was: addr_spec = "#{local_part}\\x40#{domain}"
  addr_spec = Regexp.new("#{local_part}\\x40#{domain}", nil, 'n')

  pattern = /\A#{addr_spec}\z/
end.freeze
spickermann
  • 100,941
  • 9
  • 101
  • 131