如何检测EMAIL地址是否合法?

tangyuyong 北京众智益华科技有限公司 研发总监  2001-07-21 11:46:45
请高手指教!
...全文
190 点赞 收藏 15
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wybm 2001-09-12

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function emailCheck (emailStr) {

var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
}
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
}
}
if (user.match(userPat)==null) {
alert("The username doesn't seem to be valid.");
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
}
}
return true;
}
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
}
}
if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}
if (len<2) {
alert("This address is missing a hostname!");
return false;
}
return true;
}
//-->End
</script>
</HEAD>
<BODY>
<div align=center>
<form name=emailform onSubmit="return emailCheck(this.email.value)">
Your Email Address: <input type=text name="email"><br>
<input type=submit value="Submit">
</form>
</div>
回复
crank_crank 2001-09-12
上面说的都有道理,简单+效率的话就是检测@和.(再加上后缀也可以),处理字符串比检测是否回复有效要快多了。
呵呵…………
回复
7758iloveu 2001-09-12
<script language=javascript>
function check()
{

if(document.frm.email.value=="")
{
alert("请填写Email!");
document.frm.email.focus();
return false;
}

if(document.frm.email.value.indexOf("@")<1)
{
alert("Email错误!");
return false;
}
}
</script>
<FORM METHOD=POST ACTION="test.asp" onsubmit="return check()" name="frm">
Please input your Email:<input type="text" name="email">
<input type=submit value="Submit">

</form>
回复
tangyuyong 2001-07-24
关注中...
回复
freezwy 2001-07-22
哈哈,正则表达式就是这样的。但是未免太繁了。
强烈建议同时使用客户端和服务器端的检测,这样就安全了。
回复
springsrain 2001-07-21
检测@和.啦!
回复
gottazhang 2001-07-21
一般情况是发送一封信件测试它的返回吗,如是250则表示合法。
可以当一个地址验证的工具呀。
回复
Bardo 2001-07-21
function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat)==null) {
alert("The username doesn't seem to be valid.");
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
alert("The domain name doesn't seem to be valid.");
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if (domArr[domArr.length-1].length<2 ||
domArr[domArr.length-1].length>3) {
alert("The address must end in a three-letter domain, or two letter country.");
return false;
}
if (len<2) {
var errStr="This address is missing a hostname!";
alert(errStr);
return false;
}
return true;
}
回复
freezwy 2001-07-21
当然上面的方法仅仅是检测邮件地址的合法性,不能确认真实有效,如果需要真实有效的判断,你就需要使用SOCKET编程了,这个在C#或者ASP.NET里很简单的。
对于你说的简单,我想,最简单的还是使用正则表达式,和上面我给你的第二个差不多,上面的那几个是我经常用的,主要是场合不同,使用的函数不一样,但是殊途同归!
回复
tangyuyong 2001-07-21
没人理我??
回复
tangyuyong 2001-07-21
还有更简单的方法吗?
回复
tangyuyong 2001-07-21
谢谢
回复
freezwy 2001-07-21
<script language="javascript">
<!--
function Check_Email(string){
var str_len = string.length;
if (str_len<=5){
return true
}
for(i=0;i<str_len;i++){
if (string.charCodeAt(i)>127){
return true
}
}
if (string.indexOf("@")<2){
return true
}
if (string.indexOf(".")==1){
return true
}
if (string.indexOf(":")!=-1){
return true
}
}
//-->
</script>
回复
freezwy 2001-07-21
<script language="javascript">
<!--
function checkform()
{
var filter=/^[a-zA-Z][A-Za-z0-9_.-]+@[a-zA-Z0-9_]+?\.[a-zA-Z]{2,3}$/;
if (!filter.test(document.smail.femail.value))
{
alert("****这个电子邮件地址错误****\n如果明显错误,请审核其他资料,不全者可以考虑删除!");
document.smail.femail.focus();
return false;}
return true;
}
//-->
</script>
回复
freezwy 2001-07-21
<%
function IsValidEmail(email)

dim names, name, i, c

'Check for valid syntax in an email address.

IsValidEmail = true
names = Split(email, "@")
if UBound(names) <> 1 then
IsValidEmail = false
exit function
end if
for each name in names
if Len(name) <= 0 then
IsValidEmail = false
exit function
end if
for i = 1 to Len(name)
c = Lcase(Mid(name, i, 1))
if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
IsValidEmail = false
exit function
end if
next
if Left(name, 1) = "." or Right(name, 1) = "." then
IsValidEmail = false
exit function
end if
next
if InStr(names(1), ".") <= 0 then
IsValidEmail = false
exit function
end if
i = Len(names(1)) - InStrRev(names(1), ".")
if i <> 2 and i <> 3 then
IsValidEmail = false
exit function
end if
if InStr(email, "..") > 0 then
IsValidEmail = false
end if

end function
%>
回复
相关推荐
发帖
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
帖子事件
创建了帖子
2001-07-21 11:46
社区公告
暂无公告