1

I am currently trying to write a regex code for the following rules in my javascript.

  1. E-mail address must be a valid address ( X @ Y . Z )
  2. Allowed characters for the name part is uppercase and lowercase a-z, digits, dash and dot characters
  3. Allowed characters for the domain part is only letters and dot (.).

How could I write whole cases in one regular expression?

What I did so far is /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

Thanks in advance.

A. Mesut Konuklar
  • 611
  • 3
  • 12
  • 29
  • 7
    Don't worry about it, simply use `/[^@]+@[^@]/` then send a confirmation email. – elclanrs Nov 06 '13 at 22:20
  • 2
    So according to rule 3 people who work `@1and1.com` can't sign up? – Jason Sperske Nov 06 '13 at 22:24
  • I've checked that page, but no. It's not doing what I'm exactly trying to do – A. Mesut Konuklar Nov 06 '13 at 22:30
  • That page has all the popular email regex, that's what you need if you really want to use regex, but IMO it's a waste of time. I've had complaints from users on a few occasions because their email wouldn't validate (due to a bad regex). Email confirmation is the **only** way to validate a real email. – elclanrs Nov 06 '13 at 22:33
  • Well, I know thats not quite possible. As I said in the below, I was just trying to improve myself with different examples and I just got stucked on this one. Well, still thanks. – A. Mesut Konuklar Nov 06 '13 at 22:35

1 Answers1

2

Actually validating email addresses correctly (i.e. according to the spec) is pretty hard, see e.g. http://forums.asp.net/post/1801198.aspx.

Even then, you cannot be sure that the email address is valid, you will know that it is well-formed but that doesn't mean it exists. So you probably want to send an activation message to verify that. Then, if you are going to do that anyway, why bother with complicated regexes - if the address is invalid you will immediately get a bounce.

If you still insist on using a regex, what you have looks nice but not very human-readable. So maybe you can provide more information, in that case, on what it doesn't do that you did expect it to do.

Community
  • 1
  • 1
CompuChip
  • 9,143
  • 4
  • 24
  • 48
  • Thanks for the comment, but I am not going to integrate or use it on any system. I am just trying to improve myself on regex and on this one I just got stucked. Thats why I wanted to help of you guys. @CompuChip – A. Mesut Konuklar Nov 06 '13 at 22:32
  • No problem, as long as you are aware that in "real" life it's a bit more involved. Now I'll be happy to help you fix your regex, if you can say what is wrong with it. E.g. what e-mail address fails while you would have expected it to pass, or vice versa? I would expect, on first reading, that you would want to include `\w` in the dot match, e.g. `^(\w|\w+([\.\w-]*\w+)` instead of `\w+([\.-]?\w+`. Otherwise you will only match "." but not multiple dots. – CompuChip Nov 06 '13 at 22:39
  • (Actually you have no requirement saying that the part before the `@` can't be `.....`, so just `[\w\.-]+` should do.) – CompuChip Nov 06 '13 at 22:46
  • 1
    Understood, thank you very much :) Trying to write in longer and shorter way. Should help me to understand it better – A. Mesut Konuklar Nov 06 '13 at 23:03