为什么不接受@ 后带有连字符的电子邮件地址? [英] Why won't this accept email addresses with a hyphen after the @?

查看:97
本文介绍了为什么不接受@ 后带有连字符的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是注册以在我们的网站上接收电子邮件新闻更新的代码.效果很好,除非电子邮件地址在@ 后面有一个连字符,否则它会抛出一个错误框:

This is the code to sign up to receive email news updates on our site. It works great, unless the email address has a hyphen after the @, then it throws an error box:

> The page at http://www.MySite.com says:
> 
> Please Fix the following: 
> 
> E-mail

test-test@test.com 有效,但 test@test-test.com 无效.有任何想法吗?

test-test@test.com works, but test@test-test.com does not. Any ideas?

来自 email.js:

    function checkSubmit(thisDept){

    var email = $('email').value;


    var message = "Please Fix the following: \r\n";




    var emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    if( !email.match(emailpat) ) {
        message += "E-mail \r\n";
    }





    if(message == "Please Fix the following: \r\n"){

            submitEmail(email,thisDept);

    } else {
            alert(message);
    }



    }


function submitEmail(email,thisDept){
        new Ajax.Updater( "EAlertContainer","php/ealerts.php", {
     method: 'post', parameters: { email: email } });
}

感谢您对我发布的原始代码漏洞的所有关注.所以我确定,逃避它的正确方法是:

Thanks for all of your concerns about the vulnerability of the original code I posted. Just so I'm certain, the proper way to escape it is:

mysql_select_db("DATABASE",$dbx); 
$email=mysql_real_escape_string ($email); 
$queryx = "INSERT into master_email (emailid,cellmail,email) VALUES ('','','".$email."')";

是/否?

推荐答案

试试这个:

var emailpat = /^[^@]+@[^@]+\.[^@\.]{2,}$/;

电子邮件地址应该只有一个 @ 符号,并且不能是第一个字符.在@ 之后,您至少需要一个点,后跟 2 个或更多字母.

Email addresses should have just one @-sign, and that can't be the first character. After the @ you'll need at least one dot followed by 2 or more letters.

是的,这也接受无效的电子邮件地址.如果您想确保用户输入了有效的电子邮件地址,您应该向该地址发送一封电子邮件并等待用户采取行动(即输入电子邮件中的代码).

And yes, this also accepts email addresses that are not valid. If you want to be sure that the user enters a valid email address, you should send an email to the address and wait for the user to take action (ie enter a code that's in the email).

编辑

更新了正则表达式,使域部分不再局限于罗马字母 TLD.允许使用其他字母,但可能不是很常见(目前).参见维基百科 示例.

Updated regex so the domain part is no longer restricted to roman alphabet TLDs. Other alphabets are allowed, although probably not very common (yet). See wikipedia for examples.

这篇关于为什么不接受@ 后带有连字符的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆