Ghost全自动系统备份光盘正式版 V4.5 | 硬盘版 V2.0 | 排行榜 TOP50 | 图文推荐 | 玩小游戏
首页 >> Asp编程 >> Asp新手入门 >> asp检测表单输入EMAIL合法性的函数

asp检测表单输入EMAIL合法性的函数

作者:天空诚整理 来源:中国Asp之家 时间:2007-10-16 网友评论条 【

表单验证做网站程序多多少少都会碰到,其中emai地址的合法性验证也算是一个典型的例子,网页表单的验证我们一般是先在客户端使用javascript代码来处理,这样可以为减少服务器的负担,当然一些重要的表单项我们还应该在服务器端也加上验证。

下面是表单email地址的验证,使用javascript代码:

if (document.form1.email.value.charAt(0)=="." ||       
         document.form1.email.value.charAt(0)=="@"||      
         document.form1.email.value.indexOf('@', 0) == -1 ||
         document.form1.email.value.indexOf('.', 0) == -1 ||
         document.form1.email.value.lastIndexOf("@")==document.form1.email.value.length-1 ||
         document.form1.email.value.lastIndexOf(".")==document.form1.email.value.length-1)
     {
      alert("Email地址格式不正确!");
      document.form1.email.focus();
      return false;
      }

下面是asp(vbscript)服务器端验证的函数(呵呵,代码好像有点长):

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

站长工具
关键字排名查询:关键字 网址
相关文章
loading 请稍等,评论加载中...

Aspxhome.com. 中国Asp之家. 版权所有

闽ICP备06017341号