0

Just tested this function to validate an email..However,it does not validate the presence or absence of the dot in the email..Why this is happening and how can I fix this?

<script type="text/javascript">
  function isEmail(textbox){

    var reg4 =/^(\w+)@(\w+).(\w+)$/;
    if(reg4.test(textbox.value)){

            return true;
        }

    return false;
}
</script>
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102
  • simply google: email validation regular expression and you will get a ton of solutions – Ibu Jul 19 '11 at 07:17
  • 1
    it will at least fail `info@some-domain-with-dashes.com`, `info@mail.university.edu`, `info@localhost`, `john.smith@microsoft.com`... these are all legit email addresses. – Salman A Jul 19 '11 at 07:19
  • Here is an example of RFC822 validating perl regexp which still cant handle comments: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html . So you can imagine how fun validating an email is :D – bezmax Jul 19 '11 at 07:26
  • 1
    @lbu every one of which will be wrong... – Alnitak Jul 19 '11 at 07:38
  • possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Qantas 94 Heavy Oct 16 '13 at 00:57

5 Answers5

3

No, it's insufficient.

In particular, it doesn't handle all these (legal) variants:

  1. quoted user parts, e.g. "my name"@example.com

  2. Internationalised domain names (IDNs)

  3. Domains with more than two labels

  4. Domains with only one label (yes, those are legal)

  5. Hyphens in domain names

  6. user@[1.2.3.4]

Validating an e-mail address via a regexp is very hard. See section 3.4.1 of RFC 5322 for the formal syntax definition.

Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
2

Do not use a regular expression to validate email addresses. Email addresses can include a lot of things you wouldn't imagine (like spaces), and it's more likely that the user will accidentally enter an invalid email address than an invalid one. Just check if there's an @ then send a confirmation email if you want.

From an answer to a related question:

There is no good (and realistic, see the fully RFC 822 compliant regex) regular expression for this problem. The grammar (specified in RFC 5322) is too complicated for that. Use a real parser or, better, validate by trying (to send a message).

Community
  • 1
  • 1
Jeremy
  • 1
  • 85
  • 340
  • 366
  • There you go: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html . However it still does not support comments in email address :D – bezmax Jul 19 '11 at 07:28
1

the dot has a meaning in a regex

use [\.] instead of .

Fender
  • 3,055
  • 1
  • 17
  • 25
1

You need to escape the dot, which normally matches any character.

var reg4 =/^(\w+)@(\w+)\.(\w+)$/;
Digital Plane
  • 37,354
  • 7
  • 57
  • 59
1

Escape the dot with backslash: \.. However, your regex would not be very good afterwards as it will not accept multiple dots in post-@ part, such as domain foo.co.uk.

All in all, I'd advise againts using regexes to validate emails as they tend to get overcomplicated ;-) Simply check for presence of @ followed by a dot, or use a similarly lenient algorithm.

Xion
  • 22,400
  • 10
  • 55
  • 79