开发者

RegEx problem with IE7 when trying validate Email address

开发者 https://www.devze.com 2023-04-06 18:29 出处:网络
I am wondering an annoying problem with Email Validation using regex. I\'m using this regex to validate email address on a web page:

I am wondering an annoying problem with Email Validation using regex. I'm using this regex to validate email address on a web page:

^\s*[a-zA-Z0-9_\+-]{1,63}(\.[a-zA-Z0-9_\+-]{1,63})*@(?=[a-zA-Z0-9-\.]{0,255}\.?\s*$)[a- zA-Z0-9-]{1,63}(\.[a-zA-Z0-9-]{1,63}){0,126}\.([a-zA-Z]{2,63})\.?\s*$

Now this works allright with IE8 --> and latest Mozilla and Opera version for example. I already read about this article: http://blog.stevenlevithan.com/archives/regex-lookahead-bug

But even I was using .* with ?= I could not get it working. Any RegEx guru have ideas what I'm doing wrong. I also tried this:

^\s*[a-zA-Z0-9_\+-]{1,63}(\.[a-zA-Z0-9_\+-]{1,63})*@(?=.*[a-zA-Z0-9-开发者_运维问答\.]{0,255}\.*?\s*$)[a- zA-Z0-9-]{1,63}(\.[a-zA-Z0-9-]{1,63}){0,126}\.([a-zA-Z]{2,63})\.*?\s*$

But with no success.


Is the space in the last group of

@(?=[a-zA-Z0-9-\.]{0,255}\.?\s*$)[a- zA-Z0-9-]{1,63}

right after the - intentional? This probably won't work with a conservative engine.


I got this regex and it seems to do the job right:

const string myRegex = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
        + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
        + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
        + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";


Have you tried removing the lookahead altogether? I agree with @Gerben that it serves no purpose in your regex:

^\s*[a-zA-Z0-9_\+-]{1,63}(\.[a-zA-Z0-9_\+-]{1,63})*@[a-zA-Z0-9-]{1,63}(\.[a-zA-Z0-9-]{1,63}){0,126}\.([a-zA-Z]{2,63})\.?\s*$
0

精彩评论

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

关注公众号