网络编程
位置:首页>> 网络编程>> Asp编程>> asp如何判断一个电子信箱格式是否有效?

asp如何判断一个电子信箱格式是否有效?

 来源:asp之家 发布时间:2010-01-12 20:18:00 

标签:电子信箱,格式,判断

很多用户在网站上会糊弄填写一个电子信箱,请问有什么办法可以阻止这种行为?

我们通常用两种方法来进行判断:

第一种,设定只有形如aspxhome@***.net、wendahu@***.***.net、wuxiaotao@***.***.***.net格式的信箱才符合要求,其它为错:


<script language="JavaScript1.2">
function checkmail(mail)
  var strr;
  re=/(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
  re.exec(mail);
  if (RegExp.$3!=""&&RegExp.$3!="."&&RegExp.$2!=".") strr=RegExp.$1+RegExp.$2+RegExp.$3
  else
    if (RegExp.$2!=""&&RegExp.$2!=".") strr=RegExp.$1+RegExp.$2
    else  strr=RegExp.$1
  if (strr!=mail) {alert("哎呀,写错了!重写.;return false}
  return true;
}
</script>

第二种,一般来说,判断一个电子信箱格式是否有效,最简单的方法就是检查它里面是否含有“@”和“.”以及“.”是否排在“@”后面:

lAt = False  
 lDot = false     
 for x = 2 to len(pInstring)-1
     if mid(pInString,x,1) = "@" then lAt = True
     if mid(pInString,x,1) = "." and lAt = True then lDot = True  
 next   
     if lAt = True and lDot = True then     
 isEmail = True  
 else
     isEmail = False  
 end if 
end function

这个办法还可以作为对类似的有一定格式的东西进行判断的范本。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com