ASP函数集

由于工作的关系,我已经有将近半年时间没去动过asp的东东了,不过一些资料还保留在机子上,ASP函数在实际程序中的运用很实际,能解决你很多问题,我把我自己收藏的一些,还有自己写的一些函数给弄出来,希望能对大家有用:)

这里是一个完整的PDF格式的ASP函数集下载文件 点击下载此文件


1.'判断文件名是否合法

<%
Function isFilename(aFilename)
Dim sErrorStr,iNameLength,i
isFilename=TRUE
sErrorStr=Array("/","\",":","*","?","""","<",">","|")
iNameLength=Len(aFilename)
If iNameLength<1 Or iNameLength=null Then
  isFilename=FALSE
Else
  For i=0 To 8
   If instr(aFilename,sErrorStr(i)) Then
    isFilename=FALSE    
   End If
  Next
End If
End Function
%>

2.去掉字符串头尾的连续的回车和空格
<%
function rtrimVBcrlf(str)
dim pos,isBlankChar
pos=len(str)
isBlankChar=true
while isBlankChar and pos>=2
  if mid(str,pos,1)=" " then
   pos=pos-1
  elseif mid(str,pos-1,2)=VBcrlf then
   pos=pos-2
  else
   isBlankChar=false
  end if
wend
rtrimVBcrlf=rtrim(left(str,pos))
end function
%>

3.去掉字符串开头的连续的回车和空格
<%
function ltrimVBcrlf(str)
dim pos,isBlankChar
pos=1
isBlankChar=true
while isBlankChar
  if mid(str,pos,1)=" " then
   pos=pos+1
  elseif mid(str,pos,2)=VBcrlf then
   pos=pos+2
  else
   isBlankChar=false
  end if
wend
ltrimVBcrlf=right(str,len(str)-pos+1)
end function
%>

4.判断Email是否有效,返回1表示正确
<%
Function isEmail(aEmail)
Dim iLocat,v,iLength,i,checkletter
If instr(aEmail,"@") = 0 Or instr(aEmail,".") = 0 Then
  isEmail=0
  EXIT FUNCTION
End If
iLocat=instr(aEmail,"@")
If instr(iLocat,aEmail,".")=0 Or instr(iLocat+1,aEmail,"@")>0 Then
  isEmail=0
  EXIT FUNCTION
End If
If left(aEmail,1)="." Or right(aEmail,1)="." Or left(aEmail,1)="@" Or right(aEmail,1)="@" Then
  isEmail=0
  EXIT FUNCTION
End If
v="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.@"
iLength=len(aEmail)
For i=1 To iLength
  checkletter=mid(aEmail,i,1)
  If instr(v,checkletter)=0 Then
   isEmail=0
   EXIT FUNCTION
  End If
Next
isEmail=1
End Function
%>

5.用HTML格式显示文本
<%
function HTMLEncode(fString)
if not isnull(fString) then
    fString = replace(fString, ">", ">")
    fString = replace(fString, "<", "<")

    fString = Replace(fString, CHR(32), " ")
    fString = Replace(fString, CHR(34), """)
    fString = Replace(fString, CHR(39), "'")
    fString = Replace(fString, CHR(13), "")
    fString = Replace(fString, CHR(10) & CHR(10), "

")
    fString = Replace(fString, CHR(10), "
")
    HTMLEncode = fString
end if
end function
%>

6.检查邮件
<%
Function CheckEmail(strEmail)
    Dim re
    Set re = New RegExp
    re.Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
    re.IgnoreCase = True
    CheckEmail = re.Test(strEmail)
End Function
%>

7.检查无效字符
<%
Function CheckStr(byVal ChkStr)
Dim Str:Str=ChkStr
Str=Trim(Str)
If IsNull(Str) Then
  CheckStr = ""
  Exit Function
End If
Dim re
Set re=new RegExp
re.IgnoreCase =True
re.Global=True
re.Pattern="(\r\n){3,}"
Str=re.Replace(Str,"$1$1$1")
Set re=Nothing
Str = Replace(Str,"'","''")
Str = Replace(Str, "select", "select")
Str = Replace(Str, "join", "join")
Str = Replace(Str, "union", "union")
Str = Replace(Str, "where", "where")
Str = Replace(Str, "insert", "insert")
Str = Replace(Str, "delete", "delete")
Str = Replace(Str, "update", "update")
Str = Replace(Str, "like", "like")
Str = Replace(Str, "drop", "drop")
Str = Replace(Str, "create", "create")
Str = Replace(Str, "modify", "modify")
Str = Replace(Str, "rename", "rename")
Str = Replace(Str, "alter", "alter")
Str = Replace(Str, "cast", "cast")
CheckStr=Str
End Function
%>

8.转换HTML代码
<%
Function HTMLEncode(reString)
Dim Str:Str=reString
If Not IsNull(Str) Then
  Str = UnCheckStr(Str)
  Str = Replace(Str, "&", "&")
  Str = Replace(Str, ">", ">")
  Str = Replace(Str, "<", "<")
  Str = Replace(Str, CHR(32), " ")
     Str = Replace(Str, CHR(9), "    ")
  Str = Replace(Str, CHR(9), "    ")
  Str = Replace(Str, CHR(34),""")
  Str = Replace(Str, CHR(39),"'")
  Str = Replace(Str, CHR(13), "")
  Str = Replace(Str, CHR(10), "
")
  HTMLEncode = Str
End If
End Function
%>


9.脏字过滤功能
<%
Function DelDirty(str)
str=replace(str,"妈的","MD")
str=replace(str,"靠","KAO")
DelDirty=str
End Function
%>


10.获取IP地址
<%
Function Userip()
    Dim GetClientIP
    '如果客户端用了代理服务器,则应该用ServerVariables("HTTP_X_FORWARDED_FOR")方法
    GetClientIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If GetClientIP = "" or isnull(GetClientIP) or isempty(GetClientIP) Then
        '如果客户端没用代理,应该用Request.ServerVariables("REMOTE_ADDR")方法
        GetClientIP = Request.ServerVariables("REMOTE_ADDR")
    end if
    Userip = GetClientIP
End function
%>


11.判断数字是否整形
<%
function isInteger(para)
on error resume next
dim str
dim l,i
if isNUll(para) then
isInteger=false
exit function
end if
str=cstr(para)
if trim(str)="" then
isInteger=false
exit function
end if
l=len(str)
for i=1 to l
if mid(str,i,1)>"9" or mid(str,i,1)<"0" then
isInteger=false
exit function
end if
next
isInteger=true
if err.number<>0 then err.clear
end function
%>

12.得到浏览器目前的URL
<%
Function GetCurURL()
    If Request.ServerVariables("HTTPS") = "on" Then
        GetCurrentURL = "https://"
    Else
        GetCurrentURL = "http://"
    End If
    GetCurURL = GetCurURL & Request.ServerVariables("SERVER_NAME")
    If (Request.ServerVariables("SERVER_PORT") <> 80) Then GetCurURL = GetCurURL & ":" & Request.ServerVariables("SERVER_PORT")
    GetCurURL = GetCurURL & Request.ServerVariables("URL")
    If (Request.QueryString <> "") Then GetCurURL = GetCurURL & "?" & Request.QueryString
End Function
%>


13.测试变量是否为空值
<%
Function IsBlank(ByRef Var)
    IsBlank = False
    Select Case True
        Case IsObject(Var)
            If Var Is Nothing Then IsBlank = True
        Case IsEmpty(Var), IsNull(Var)
            IsBlank = True
        Case IsArray(Var)
            If UBound(Var) = 0 Then IsBlank = True
        Case IsNumeric(Var)
            If (Var = 0) Then IsBlank = True
        Case Else
            If Trim(Var) = "" Then IsBlank = True
        End Select
End Function
%>

14.用于取代request.Form,request.QueryString
<%
Function SafePost(objForm)
Dim strContent
strContent = Trim(Request.Form(objForm))
strContent = Replace(strContent,"""","")
strContent = Replace(strContent,"'","")
SafePost = strContent
End function
Function SafeGet(objForm)
Dim strContent
strContent = Trim(Request.QueryString(objForm))
strContent = Replace(strContent,"""","")
strContent = Replace(strContent,"'","")
SafeGet = strContent
End function

%>




上一篇: 生活不是程序 代码不是人生
下一篇: ASP函数集(2)
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 3 | 引用: 738 | 查看次数: 36954
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭