【问题标题】:Email Validation... advocating the use of +, and how embarrassed would you be to have written this?电子邮件验证... 提倡使用 +,如果你写了这个,你会有多尴尬?
【发布时间】:2011-02-15 14:50:37
【问题描述】:

我充分利用了 GMail 的通配符功能(用户名+wildcard@gmail.com)。不幸的是,大多数开发人员似乎不明白+ 在电子邮件地址中是有效的。这使得有时尝试取消订阅成为一件苦差事。

TicketMaster 为例...您会立即注意到他们甚至没有转义电子邮件地址,因此文本字段默认为“user wilcard@gmail.com”。没问题,我们可以手动添加+。单击提交后,您会注意到验证使您停在原地。现在呢?

大多数用户将不得不进一步联系 TicketMaster 并尝试解释情况。我打开 FireBug 进行调查。就在那时,我注意到这个庞大的 74 行 电子邮件验证功能具有如此多的冗余,这太荒谬了。我最喜欢的支票在第 20 行,通知用户他/她的电子邮件 cannot have more than one @。虚幻。我第二喜欢的部分是使用的两个正则表达式!

想象一下...有人为此付了钱...从表面上看,他们是按行数付钱的。

//Validates the email
function validateOptoutEmail(object) {
    var emailStr = object.value;
    if(emailStr == '' || emailStr == null) {
        alert('Email can not be empty. Please provide email');
        object.value = '';
        object.focus();
        return false;
    } else if(Trim(emailStr).length == 0) {
        alert('Email can not be empty. Please provide email');
        object.value = '';
        object.focus();
        return false;
    } else {
        var atcount=0;
        for(var i=0;i<emailStr.length;i++) {
            if(emailStr.charAt(i)=='@') atcount++;
        }
        if(atcount>1) {
            alert('Invalid email. Email cannot have more than one @');
            object.value = '';
            object.focus();
            return false;
        }

        if(emailStr.indexOf('.') == -1) {
            alert('Invalid email. Email must have one dot');
            object.value = '';
            object.focus();
            return false;
        }
        if(emailStr.indexOf('..')!= -1) {
            alert('Invalid email. Email cannot have consecutive dots');
            object.value = '';
            object.focus();
            return false;
        }
        var dotpos=0;
        for(var i=dotpos;i< emailStr.length;i++) {
            var ichar=emailStr.charAt(i);
            if(ichar=='.') dotpos=i;
        }
        for(var i=dotpos+1;i< emailStr.length;i++) {
            var ichar=emailStr.charAt(i);
            if((!isNaN(ichar)) || (ichar == '_')) {
                alert('Invalid email. Email cannot have numbers or _ after dot');
                object.value = '';
                object.focus();
                return false;
            }
        }
        var pattern2=/^([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
        var pattern1=/^[0-9a-zA-Z\-\_.]+@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
        if (pattern1.test(emailStr)) {
            if(pattern2.test(emailStr)) {
                return true;
            } else {
                alert('Invalid email');
                object.value = '';
                object.focus();
            }
            return true;
        } else {
            alert('Invalid email');
            object.value = '';
            object.focus();
            return false;
        }
        alert('Invalid email');
        object.value = '';
        object.focus();
        return false;
    }
}

我最终只是在 FireBug 中设置了一个断点,并更改了传递给验证函数的电子邮件地址的值。从那里一切正常...

说了这么多,我们如何才能知道+ 在电子邮件地址中有效? 我经常无法使用我想使用的电子邮件地址对于某些网站,因为开发人员根本不知道什么是有效的电子邮件地址。

【问题讨论】:

  • “愚蠢,你征服了,我必须屈服!与愚蠢抗争的众神自己都是徒劳的。” ——弗里德里希·席勒

标签: javascript email-validation


【解决方案1】:

将它们指向 rfc: https://www.rfc-editor.org/rfc/rfc5322#page-10

3.2.3 声明“+”是一个有效的原子

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 2017-10-21
    • 2014-01-26
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2011-09-10
    • 2013-11-04
    • 2013-10-14
    相关资源
    最近更新 更多