最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regular expression with if statements - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question edited Oct 22, 2012 at 21:14 Daedalus 7,7225 gold badges38 silver badges63 bronze badges asked Oct 22, 2012 at 21:06 user1766599user1766599 111 silver badge4 bronze badges 2
  • 1 + is a valid character in an email address. And some tlds are more than 4 characters (e.g. .museum). – huon Commented Oct 22, 2012 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 Commented Oct 22, 2012 at 21:14
Add a ment  | 

4 Answers 4

Reset to default 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().

I see that you have jquery 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?

Validating emails is hard. The fully correct regex is a true monstrosity that you can see (if you dare) at http://www.ex-parrot./~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");   
}

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

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

That should also handle the .museum case

发布评论

评论列表(0)

  1. 暂无评论