我充分利用了 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 中设置了一个断点,并更改了传递给验证函数的电子邮件地址的值。从那里一切正常...
综上所述,我们如何才能+
在电子邮件地址中找到有效的单词? 很多时候,我无法使用我想用于某些网站的电子邮件地址,因为开发人员根本不知道什么是有效的电子邮件地址。