1

I have this as my regular expression:

var email = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

And this is my if statement:

if($('#email').val() ==""){  
    $('#emailErrorMsg').text("Please enter a valid email address.");
}  
else if(!email.test('#email')) {   
    $('#emailErrorMsg').text("OK");   
}  
else($('#emailErrorMsg').text("Please enter a valid email address."));  
});

When I type in a valid email address it says "OK". However, if I enter just some text for example it still says "OK" when I want it to say "Please enter a valid email address". Anyone any idea. By the way, I'm still an amatuer at this stuff!

Daedalus
  • 7,586
  • 5
  • 36
  • 61
user1766599
  • 11
  • 1
  • 4
  • 1
    `+` is a valid character in an email address. And some tlds are more than 4 characters (e.g. `.museum`). – huon Oct 22 '12 at 21:11
  • 2
    Why the `?` at the very end? It seems the entire pattern is thus optional. "Match all of this... or not." – Wiseguy Oct 22 '12 at 21:14

4 Answers4

2

The main problem is that you have a ? at the end of the regex, following parentheses that enclose the entire pattern. This effectively makes the entire match optional, so the regex will literally match anything.

Note also that you are testing the literal string #email, not the value of the #email element. Make sure you pass the appropriate string to test().

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

I see that you have tag, so take a look to JQuery validate plugin, it will be better than a simple regex.

But if you still want regex, see Validate email address in JavaScript?

Community
  • 1
  • 1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

Validating emails is hard. The fully correct regex is a true monstrosity that you can see (if you dare) at http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html which probably isn't what you want.

Instead, you have a few options. Use a regex that matches 99% of emails, do it server side with an email validation library, or implement a finite state machine to parse it correctly. The state machine is probably too bulky (although allows neat stuff like suggestions for possible typos) and doing it all server side -- which you better be doing anyway (what if someone has JavaScript disabled?) -- loses the benefits of as-you-type checking.

That leaves a simpler regex that doesn't match all legal emails, but matches enough that the chances of someone registering with one that it doesn't are really slim. The regex from Validate email address in JavaScript? should do the trick pretty well:

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

Also, you made a small typo:

else if(!email.test('#email')) {   
    $('#emailErrorMsg').text("OK");   
}

is testing against the string '#email' -- not the element with the ID 'email'. Change that to:

else if(!email.test($('#email').val())) {   
    $('#emailErrorMsg').text("OK");   
}
Community
  • 1
  • 1
Peter C
  • 6,219
  • 1
  • 25
  • 37
0

There's a little typo in your regex. Try this:

var email = /^([\w-\.]+)@([\w-]+\.)+[\w-]{2,6}?$/;

That should also handle the .museum case

raykendo
  • 620
  • 1
  • 12
  • 25
  • that's what this part of the regex is supposed to handle: @([\w-]+\.)+ any letters or dashes, followed by a period, one or more times, which includes mail.domain.com – raykendo Oct 22 '12 at 21:30