开发者

Regular expression for email [duplicate]

This question already has answers here: 开发者_StackOverflow社区 Closed 12 years ago.

Possible Duplicate:

What is the best regular expression for validating email addresses?

I tried the reg expression

^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+

for the email validation.

Since I want the user to allow submitting even with the empty email address. So I changed the reg ex to

(^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+)?

But this expression accepts any email address without any validation.


Thanks Konerak!!!! I changed the expression to

^(([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+)?$

and this is working for me.


Welcome. http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html


How to: Verify That Strings Are in Valid E-Mail Format


A good bit of info on doing this is available at this page.

As that page will tell you, it's extremely difficult to pass any and all RFC822-compliant addresses with a regex. You need to ask yourself just how important this is to your application.

Personally I would recommend, if applicable, simply asking users to enter the email address twice and then confirm with a confirmation email. That way you don't run the risk of erroneously rejecting a valid email address (an extremely annoying situation for users which will likely lose your site some customers).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜