user1766599
user1766599

Reputation: 11

regular expression with if statements

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!

Upvotes: 1

Views: 6697

Answers (4)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324710

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().

Upvotes: 2

Peter C
Peter C

Reputation: 6307

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");   
}

Upvotes: 1

raykendo
raykendo

Reputation: 640

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

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

That should also handle the .museum case

Upvotes: 0

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185434

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?

Upvotes: 1

Related Questions