函数功能:防止SQL注入
'参 数:StrValue 用户提交的数据,BloType 数据类型
'参 数 值: true 数值型数据,false 字符型数据
'-----------------------------------------------------------------
程序代码
function FunSQL(StrValue,BloType)
if BloType then
if Isnumeric(StrValue) then
FunSQL=clng(StrValue)
else
StrValue=0
end if
else
if not isnull(StrValue) then
StrValue=lcase(StrValue)
StrValue=replace(StrValue,"select","")
StrValue=replace(StrValue,"update","")
StrValue=replace(StrValue,"insert","")
StrValue=replace(StrValue,"delete","")
StrValue=replace(StrValue,";","")
StrValue=replace(StrValue," ","")
StrValue=replace(StrValue,"*","")
StrValue=replace(StrValue,"%","")
StrValue=replace(StrValue,"'","")
end if
end if
FunSQL=StrValue
end function
程序代码
'-------------------------------------------------
'函数名称:HTMLEncode
'函数功能:过滤HTML代码
'-------------------------------------------------
function HTMLEncode(fString)
if not isnull(fString) then
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(9), " ")
fString = Replace(fString, CHR(34), """)
fString = Replace(fString, CHR(39), "'")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
fString = Replace(fString, CHR(10), "<BR> ")
HTMLEncode = fString
end if
end function
'HTML解码函数
程序代码
Function HTMLDecode(reString) 'HTML解码函数(保存或提交数据时使用,可以不使用)
Dim Str:Str=reString
If Not IsNull(Str) Then
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, "<br>", CHR(10))
HTMLDecode = Str
End If
End Function
程序代码
'------------------------------------------------------------
'函数功能:弹出对话框并转向指定页
'参 数:StrURL: 点击确定后返回到的页面地址;StrMSG:对话框的提示信息
'------------------------------------------------------------
Function FunMsg(StrURL,StrMSG)
Response.write "<script language='javascript'>"&_
Vbcrlf&"alert("""&StrMSG&""");"&_
Vbcrlf&"window.location="""&StrURL&""";"&_
Vbcrlf&"</script>"
End Function
程序代码
'------------------------------------------------------------
'函数功能:取得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
程序代码
'------------------------------------------------------------
'函数功能:转换IP地址
'------------------------------------------------------------
function cip(sip)
tip=cstr(sip)
sip1=left(tip,cint(instr(tip,".")-1))
tip=mid(tip,cint(instr(tip,".")+1))
sip2=left(tip,cint(instr(tip,".")-1))
tip=mid(tip,cint(instr(tip,".")+1))
sip3=left(tip,cint(instr(tip,".")-1))
sip4=mid(tip,cint(instr(tip,".")+1))
cip=cint(sip1)*256*256*256+cint(sip2)*256*256+cint(sip3)*256+cint(sip4)
end function
程序代码
'**************************************************
'函数名:IsValidEmail
'作 用:检查Email地址合法性
'参 数:email ----要检查的Email地址
'返回值:True ----Email地址合法
' False ----Email地址不合法
'**************************************************
function IsValidEmail(email)
dim names, name, i, c
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
程序代码
'**************************************************
'函数名:SendMail
'作 用:用Jmail组件发送邮件
'参 数:MailtoAddress ----收信人地址
' MailtoName -----收信人姓名
' Subject -----主题
' MailBody -----信件内容
' FromName -----发信人姓名
' MailFrom -----发信人地址
' Priority -----信件优先级
'**************************************************
function SendMail(MailtoAddress,MailtoName,Subject,MailBody,FromName,MailFrom,Priority)
on error resume next
Dim JMail
Set JMail=Server.CreateObject("JMail.Message")
if err then
SendMail= "<br><li>没有安装JMail组件</li>"
err.clear
exit function
end if
JMail.Charset="gb2312" '邮件编码
JMail.silent=true
JMail.ContentType = "text/html" '邮件正文格式
'JMail.ServerAddress=MailServer '用来发送邮件的SMTP服务器
'如果服务器需要SMTP身份验证则还需指定以下参数
JMail.MailServerUserName = MailServerUserName '登录用户名
JMail.MailServerPassWord = MailServerPassword '登录密码
JMail.MailDomain = MailDomain '域名(如果用“name@domain.com”这样的用户名登录时,请指明domain.com
JMail.AddRecipient MailtoAddress,MailtoName '收信人
JMail.Subject=Subject '主题
JMail.HMTLBody=MailBody '邮件正文(HTML格式)
JMail.Body=MailBody '邮件正文(纯文本格式)
JMail.FromName=FromName '发信人姓名
JMail.From = MailFrom '发信人Email
JMail.Priority=Priority '邮件等级,1为加急,3为普通,5为低级
JMail.Send(MailServer)
SendMail =JMail.ErrorMessage
JMail.Close
Set JMail=nothing
end function
程序代码
'**************************************************
'函数名:ReplaceBadChar
'作 用:过滤非法的SQL字符
'参 数:strChar-----要过滤的字符
'返回值:过滤后的字符
'**************************************************
function ReplaceBadChar(strChar)
if strChar="" then
ReplaceBadChar=""
else
ReplaceBadChar=replace(replace(replace(strChar,"'","??"),"<","£?"),">","£?")
end if
end function
程序代码
'取得一个不重复的序号
Function NewFileName()
dim ranNum
dim dtNow
dtNow=Now()
ranNum=int(90000*rnd)+10000
NewFileName=year(dtNow) & right("0" & month(dtNow),2) & right("0" & day(dtNow),2) & right("0" & hour(dtNow),2) & right("0" & minute(dtNow),2) & right("0" & second(dtNow),2) & ranNum
end function
程序代码
'采集分页管理
sub showpage(sfilename,totalnumber,maxperpage,ShowTotal,ShowAllPages,strUnit)
dim n, i,strTemp,strUrl
if totalnumber mod maxperpage=0 then
n= totalnumber \ maxperpage
else
n= totalnumber \ maxperpage+1
end if
strTemp= "<table align='center'><tr><td>"
if ShowTotal=true then
strTemp=strTemp & "共 <b>" & totalnumber & "</b> " & strUnit & " "
end if
strUrl=JoinChar(sfilename)
if CurrentPage<2 then
strTemp=strTemp & "首页 上一页 "
else
strTemp=strTemp & "<a href='" & strUrl & "page=1'>首页</a> "
strTemp=strTemp & "<a href='" & strUrl & "page=" & (CurrentPage-1) & "'>上一页</a> "
end if
if n-currentpage<1 then
strTemp=strTemp & "下一页 尾页"
else
strTemp=strTemp & "<a href='" & strUrl & "page=" & (CurrentPage+1) & "'>下一页</a> "
strTemp=strTemp & "<a href='" & strUrl & "page=" & n & "'>尾页</a>"
end if
strTemp=strTemp & " 页次:<strong><font color=red>" & CurrentPage & "</font>/" & n & "</strong>页 "
strTemp=strTemp & " <b>" & maxperpage & "</b>" & strUnit & "/页"
if ShowAllPages=True then
strTemp=strTemp & " 转到:<select name='page' size='1' onchange=""javascript:window.location='" & strUrl & "page=" & "'+this.options[this.selectedIndex].value;"">"
for i = 1 to n
strTemp=strTemp & "<option value='" & i & "'"
if cint(CurrentPage)=cint(i) then strTemp=strTemp & " selected "
strTemp=strTemp & ">第" & i & "页</option>"
next
strTemp=strTemp & "</select>"
end if
strTemp=strTemp & "</td></tr></table>"
response.write strTemp
end sub
function JoinChar(strUrl)
if strUrl="" then
JoinChar=""
exit function
end if
if InStr(strUrl,"?")<len(strUrl) then
if InStr(strUrl,"?")>1 then
if InStr(strUrl,"&")<len(strUrl) then
JoinChar=strUrl & "&"
else
JoinChar=strUrl
end if
else
JoinChar=strUrl & "?"
end if
else
JoinChar=strUrl
end if
end function
程序代码
'**************************************************
'函数名:strLength
'作 用:求字符串长度。汉字算两个字符,英文算一个字符。
'参 数:str ----要求长度的字符串
'返回值:字符串长度
'**************************************************
function strLength(str)
ON ERROR RESUME NEXT
dim WINNT_CHINESE
WINNT_CHINESE = (len("中国")=2)
if WINNT_CHINESE then
dim l,t,c
dim i
l=len(str)
t=l
for i=1 to l
c=asc(mid(str,i,1))
if c<0 then c=c+65536
if c>255 then
t=t+1
end if
next
strLength=t
else
strLength=len(str)
end if
if err.number<>0 then err.clear
end function
程序代码
'**************************************************
'函数名:gotTopic
'作 用:截字符串,汉字一个算两个字符,英文算一个字符
'参 数:str ----原字符串
' strlen ----截取长度
'返回值:截取后的字符串
'**************************************************
function gotTopic(str,strlen)
if str="" then
gotTopic=""
exit function
end if
dim l,t,c, i
str=replace(replace(replace(replace(str," "," "),""",chr(34)),">",">"),"<","<")
l=len(str)
t=0
for i=1 to l
c=Abs(Asc(Mid(str,i,1)))
if c>255 then
t=t+2
else
t=t+1
end if
if t>=strlen then
gotTopic=left(str,i)
exit for
else
gotTopic=str
end if
next
gotTopic=replace(replace(replace(replace(gotTopic," "," "),chr(34),"""),">",">"),"<","<")
end function
程序代码
'以下用法和len(),lift(),right()一样。
Function Strlength(Str)
Temp_Str=Len(Str)
For I=1 To Temp_Str
Test_Str=(Mid(Str,I,1))
If Asc(Test_Str)>0 Then
Strlength=Strlength+1
Else
Strlength=Strlength+2
End If
Next
End Function
Function Strleft(Str,L)
Temp_Str=Len(Str)
For I=1 To Temp_Str
Test_Str=(Mid(Str,I,1))
Strleft=Strleft&Test_Str
If Asc(Test_Str)>0 Then
lens=lens+1
Else
lens=lens+2
End If
If lens>=L Then Exit For
Next
End Function
Function Strright(Str,L)
Temp_Str=Len(Str)
For i = Temp_Str to 1 step -1
Test_Str=(Mid(Str,I,1))
Strright=Test_Str&Strright
If Asc(Test_Str)>0 Then
lens=lens+1
Else
lens=lens+2
End If
If lens>=L Then Exit For
Next
End Function
程序代码
<%
'******************************************************************
'登录验证函数:
'参数说明:
'chk_regist(requestname,requestpwd,tablename,namefield,pwdfield,reurl)
'requestname 为接受HTML页中输入名称的INPUT控件名
'requestpwd 为接受HTML页中输入密码的INPUT控件名
'tablename 为数据库中保存注册信息的表名
'namefield 为该信息表中存放用户名称的字段名
'pwdfield 为该信息表中存放用户密码的字段名
'reurl 为登录正确后跳转的页
'引用示例:call chk_regist("b_name","b_pwd","cn_admin","cn_name","cn_pwd","admin.asp")
'************************************************************************
Function chk_regist(requestname,requestpwd,tablename,namefield,pwdfield,reurl)
dim cn_name,cn_pwd
cn_name=trim(request.form(""&requestname&""))
cn_pwd=trim(request.form(""&requestpwd&""))
if cn_name="" or cn_pwd="" then
response.Write("<script language=javascript>alert(""请将帐号密码填写完整,谢谢合作。"");history.go(-1)</script>")
end if
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from "&tablename&" where "&namefield&"='"&cn_name&"'"
rs.open sql,conn,1,1
if rs.eof then
response.Write("<script language=javascript>alert(""没有该会员ID,请确认有没有被申请。"");history.go(-1)</script>")
else
if rs(""&pwdfield&"")=cn_pwd then
session("cn_name")=rs(""&namefield&"")
response.Redirect(reurl)
else
response.Write("<script language=javascript>alert(""提醒,您的帐号和密码是不吻合。注意数字和大小写。"");history.go(-1)</script>")
end if
end if
rs.close
Set rs = Nothing
End Function
%>
为很多中小企业写站点,一般产品展示是个大项目,那么做成的页面也就不同。
要不就是横排来几个,要不就是竖排来几个,甚至全站要翻来覆去的搞个好几次,麻烦也很累。
索性写个函数能缓解一下,于是就成了下面:
程序代码
<%
function showpros(tablename,topnum,fildname,loopnum,typenum)
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select top "&topnum&" * from "&tablename
rs.Open sql,conn,1,1
if rs.eof and rs.bof then
response.Write("暂时无该记录")
else
response.Write("<table width='100%'>")
for i=1 to rs.recordcount
if (i mod loopnum=1) then
response.write"<tr>"
end if
select case typenum
case "1"
response.Write("<td><table width='100%'><tr><td bgcolor=red width='50%'>")
response.Write(rs(""&fildname&""))
response.Write("</td><td bgcolor=black>")
response.Write("方式1之"&i&"记录")'此处的“方式1”可以替换显示为其余字段的值
response.Write("</td></tr>")'如果字段比较多,继续添加新个表格行来显示
response.Write("</table></td>")
case "2"
response.Write("<td><table width='100%'><tr><td bgcolor=red>")
response.Write(rs(""&fildname&""))
response.Write("</td></tr>")
response.Write("<tr><td bgcolor=black>")
response.Write("方式2之"&i&"记录")
response.Write("</td></tr>")
response.Write("</table></td>")
end select
if (i mod loopnum=0) then
response.write"</tr>"
end if
rs.movenext
next
response.Write("</table>")
end if
rs.close
Set rs = Nothing
end function
%>
参数说明:
showpros(tablename,topnum,fildname,loopnum,typenum)
whichpro为选择何类型的产品种类
topnum表示提取多少条记录
fildname表示调试显示的字段,具体应用的时候可以省去该参数,在函数内部直接使用
loopnum表示显示的循环每行的记录条数
typenum表示循环显示的方法:目前分了两类,横向并列、纵向并列显示同一数据记录行的不同记录
引用示例如下:
程序代码
<%
if request.form("submit")<>"" then
topnum=request.form("topnum")
loopnum=request.form("loopnum")
typenum=request.form("typenum")
else
topnum=8
loopnum=2
typenum=1
end if
%>
<%call showpros("cn_products",topnum,"p_name",loopnum,typenum)%>
<form action=index.asp method=post>
显示的记录总数:<input name=topnum value=<%=topnum%>>
显示的行循环数:<input name=loopnum value=<%=loopnum%>>
显示的方式类型:<select name=typenum>
<option value="1">方式1</option>
<option value="2">方式2</option>
</select>
<input type=submit name=submit value=Sure>
</form>
调式地址:http://www.cnbruce.com/test/function/index.asp
ASP安全检测与过滤函数SafeCheck
程序代码
<%
'作用:安全字符串检测函数
'名字:SafeCheck
'参数:CheckString,CheckType,CheckLength
'说明:
'Checkstring待检测字符串:任意字符.
'CheckType检测类型0正常短字符1数字2日期3金钱4编码HTML5解码HTML6登录字符串7防攻击检测
'CheckLength检测类型长度:类型为int,当为金钱时为小数点的位置
'返回值:如果通过检测,返回正确字符串,
'如果未通过则返回错误代码SYSTEM_ERROR|ERROR_CODE
'Script Writen by :SnowDu(杜雪.NET)
'Web:http://www.snsites.com/
'Web:http://www.knowsky.com/
'-------------------------------------------
function SafeCheck(CheckString,CheckType,CheckLength)
On Error Resume Next
ErrorRoot="SYSTEM_ERROR|"
if checkString="" then
SafeCheck=ErrorRoot&"00001"
exit function
end if
CheckString=Replace(CheckString,"'","'")
select case CheckType
case 0
CheckString=trim(CheckString)
SafeCheck=Left(CheckString,CheckLength)
case 1
if not isnumberic(CheckString) then
SafeCheck=ErrorRoot&"00002"
exit function
else
SafeCheck=Left(CheckString,CheckLength)
end if
case 2
tempVar=IsDate(CheckString)
if Not TempVar then
SafeCheck=ErrorRoot&"00003"
exit function
else
select case CheckLength
case 0
SafeCheck=FormatDateTime(CheckString,vbShortDate)
case 1
SafeCheck=FormatDateTime(CheckString,vbLongDate)
case 2
SafeCheck=CheckString
end select
end if
case 3
tempVar=FormatCurrency(CheckString,0)
if Err then
SafeCheck=ErrorRoot&"00004"
exit function
else
SafeCheck=FormatCurrency(CheckString,CheckLength)
end if
case 4
sTemp = CheckString
If IsNull(sTemp) = True Then
SafeCheck=ErrorRoot&"00005"
Exit Function
End If
sTemp = Replace(sTemp, "&", "&")
sTemp = Replace(sTemp, "<", "<")
sTemp = Replace(sTemp, ">", ">")
sTemp = Replace(sTemp, Chr(34), """)
sTemp = Replace(sTemp, Chr(10), "<br>")
SafeCheck = Left(sTemp,CheckLength)
case 5
sTemp = CheckString
If IsNull(sTemp) = True Then
SafeCheck=ErrorRoot&"00006"
Exit Function
End If
sTemp = Replace(sTemp, "&", "&")
sTemp = Replace(sTemp, "<", "<")
sTemp = Replace(sTemp, ">", ">")
sTemp = Replace(sTemp, """, Chr(34))
sTemp = Replace(sTemp, "<br>",Chr(10))
SafeCheck = Left(sTemp,CheckLength)
case 6
s_BadStr = "' &<>?%,;:()`~!@#$^*{}[]|+-=" & Chr(34) & Chr(9) & Chr(32)
n = Len(s_BadStr)
IsSafeStr = True
For i = 1 To n
If Instr(CheckString, Mid(s_BadStr, i, 1)) > 0 Then
IsSafeStr = False
End If
Next
if IsSafeStr then
SafeCheck=left(CheckString,CheckLength)
else
SafeCheck=ErrorRoot&"00007"
Exit Function
end if
case 7
s_Filter="net user|xp_cmdshell|/add|select|count|asc|char|mid|'|""|"
S_Filter=S_Filter&"insert|delete|drop|truncate|from|%|declare|-"
S_Filters=split(S_Filter,"|")
isFound=false
for i=0 to ubound(S_Filters)-1
if Instr(lcase(CheckString),lcase(S_Filters(i)))<>0 then
isFound=true
exit for
end if
next
if isFound then
SafeCheck=ErrorRoot&"00008"
Exit Function
else
SafeCheck=left(CheckString,CheckLength)
end if
end select
end function
%>
程序代码
<%
'************************************************
'一个把数字转英文的实用程序
'原数字格式:2000
'格式化后:TWO THOUSAND ONLY
'引用:<%=make("2000")%>
'***********************************
function zr4(y)'准备数据
dim z(10)
z(1)="ONE"
z(2)="TWO"
z(3)="THREE"
z(4)="FOUR"
z(5)="FIVE"
z(6)="SIX"
z(7)="SEVEN"
z(8)="EIGHT"
z(9)="NINE"
zr4=z(MID(y,1,1))
end function
function zr3(y)'准备数据
dim z(10)
z(1)="ONE"
z(2)="TWO"
z(3)="THREE"
z(4)="FOUR"
z(5)="FIVE"
z(6)="SIX"
z(7)="SEVEN"
z(8)="EIGHT"
z(9)="NINE"
zr3=z(MID(y,3,1))
end function
function zr2(y)'准备数据
dim z(20)
z(10)="TEN"
z(11)="ELEVEN"
z(12)="TWELVE"
z(13)="THIRTEEN"
z(14)="FOURTEEN"
z(15)="FIFTEEN"
z(16)="SIXTEEN"
z(17)="SEVENTEEN"
z(18)="EIGHTEEN"
z(19)="NINETEEN"
zr2=z(MID(y,2,2))
end function
function zr1(y)'准备数据
dim z(10)
z(1)="TEN"
z(2)="TWENTY"
z(3)="THIRTY"
z(4)="FORTY"
z(5)="FIFTY"
z(6)="SIXTY"
z(7)="SEVENTY"
z(8)="EIGHTY"
z(9)="NINETY"
zr1=z(MID(y,2,1))
end function
function dw(y)'准备数据
dim z(5)
z(0)=""
z(1)="THOUSAND"
z(2)="MILLION"
z(3)="BILLION"
dw=z(y)
end function
function w2(y)'用来制作2位数字转英文
if MID(y,2,1)="0" then'判断是否小于十
value=zr3(y)
elseif MID(y,2,1)="1" then'判断是否在十到二十之间
value=zr2(y)
elseif MID(y,3,1)="0" then'判断是否为大于二十小于一百的能被十整除的数(为了去掉尾空格)
value=zr1(y)
else
value=zr1(y)+" "+zr3(y)'加上十位到个位的空格
end if
w2=value
end function
function w3(y)'用来制作3位数字转英文
if MID(y,1,1)="0" then'判断是否小于一百
value=w2(y)
elseif MID(y,2,2)="00" then '判断是否能被一百整除
value=zr4(y)+" "+"HUNDRED"
else
value=zr4(y)+" "+"HUNDRED"+" "+"AND"+" "+w2(y)'不能整除的要后面加“AND”
end if
w3=value
end function
function make(x)
z=instr(1,x,".",1)'取小数点位置
if z<>0 then'判断有没有小数
lstr=mid(x,1,z-1)'取小数点左边的字串
rstr=mid(x,z+1,2)'取小数点右边的字串
else
lstr=x'没有小数的情况
end if
lstrev=StrReverse(lstr)'对左边的字串取反字串
dim a(5)'定义5个字串变量用来存放解析出的三位一组的字串
select case len(lstrev) mod 3'字串长度不能被整除,需补齐
case "1"
lstrev=lstrev+"00"
case "2"
lstrev=lstrev+"0"
end select
lm=""'用来存放转换后的整数部分
for i=0 to len(lstrev)/3-1'计算有多少个三位
a(i)=StrReverse(mid(lstrev,3*i+1,3))'截取第1个三位
if a(i)<>"000" then '用来避免这种情况“1000000=ONE MILLION THOUSAND ONLY”
if i<>0 then
lm=w3(a(i))+" "+dw(i)+" "+lm'用来加上“THOUSAND or MILLION or BILLION”
else
lm=w3(a(i))'防止i=0时“lm=w3(a(i))+" "+dw(i)+" "+lm”多加两个尾空格
end if
else
lm=w3(a(i))+lm
end if
NEXT
xs=""'用来存放转换后的小数部分
if z<>0 then
xs="AND CENTS"+" "+w2("$"+rstr)+" "'小数部分存在时转换小数部分
end if
make=lm+" "+xs+"ONLY"'最后结果,加上ONLY
end function
%>
程序代码
<%
'********************************
'把长的数字用逗号隔开显示
'文字格式:12345678
'格式化数字:12,345,678
'********************************
Function Comma(str)
If Not(IsNumeric(str)) or str = 0 Then
Result = 0
ElseIf Len(Fix(str)) < 4 Then
Result = str
Else
Pos = Instr(1,str,".")
If Pos > 0 Then
Dec = Mid(str,Pos)
End if
Res = StrReverse(Fix(str))
LoopCount = 1
While LoopCount <= Len(Res)
TempResult = TempResult + Mid(Res,LoopCount,3)
LoopCount = LoopCount + 3
If LoopCount <= Len(Res) Then
TempResult = TempResult + ","
End If
Wend
Result = StrReverse(TempResult) + Dec
End If
Comma = Result
End Function
%>
程序代码
<%
'每行显示n个字母,自动换行
Function rowscode(str,n)
If len(str)<=n/2 Then
rowscode=str
Else
Dim TStr
Dim l,t,c
Dim i
l=len(str)
TStr=""
t=0
for i=1 to l
c=asc(mid(str,i,1))
If c<0 then c=c+65536
If c>255 then
t=t+2
Else
t=t+1
End If
TStr=TStr&(mid(str,i,1))
If t>n Then
TStr=TStr&"<br>"
t=0
End if
next
rowscode= TStr
End If
End Function
%>
程序代码
'2004-6-11 => 二零零四年六月十一日
Function D(x)
if int(split(x,"-")(2)/10)=0 then D=D & F(split(x,"-")(2)) else _
if split(x,"-")(2) mod 10 =0 then D=D & F(int(split(x,"-")(2)/10)) & "十" else _
D=D & F(int(split(x,"-")(2)/10)) & "十" & F(split(x,"-")(2) mod 10)
D=F(split(x,"-")(0)) & "年" & MonthName(split(x,"-")(1),True) & replace(D,"一十","十") & "日"
end Function
Function F(x)
for i=1 to len(x)
if mid(x,i,1)="0" then F=F & "零" else F=F & left(MonthName(mid(x,i,1),True),1)
next
end Function
msgbox D(Date)
程序代码
'正确显示数据库里同时存在的GB码和BIG5码
Public Function CheckBIG(strSource As String) As Boolean
Dim idx As Long
Dim ByteTemp() As Byte
CheckBIG = False
For idx = 1 To Len(strSource)
ByteTemp = StrConv(Mid(strSource, idx, 1), vbFromUnicode)
If UBound(ByteTemp) > 0 Then
If (ByteTemp(1) >= 64) And (ByteTemp(1) <= 126) Then
CheckBIG = True
Exit For
End If
End If
Next idx
End Function
程序代码
'*******************************************************************
'检测是否是手机浏览
'*******************************************************************
<%
if instr(request.ServerVariables("HTTP_USER_AGENT"),"Mozilla")=0 then
response.redirect "/wap" '如果客户端为手机访问,则进入/wap目录
else
response.redirect "http://wap.fz0132.com"/ '如果客户端不是手机访问,则进入指定地址
end if
%>
程序代码
'*******************************************************************
' 获得文件扩展名
'*******************************************************************
function GetExtend(filename)
dim tmp
if filename〈>"" then
tmp=mid(filename,instrrev(filename,".")+1,len(filename)-instrrev(filename,"."))
tmp=LCase(tmp)
if instr(1,tmp,"asp")>0 or instr(1,tmp,"php")>0 or instr(1,tmp,"php3")>0 or instr(1,tmp,"aspx")>0 then
getextend="txt"
else
getextend=tmp
end if
else
getextend=""
end if
end function
程序代码
<%
'==================================================
'函数名:gb2big5
'作 用:国标码转大五码
'参 数:Text ------ 字符串
'作 者:netyum(杨嵩)
'Q Q: 8025628
'时 间:2004年6月28日
'==================================================
Function gb2big5(Text)
Dim Ados,Max,i,c,b,h,l
If IsNull(Text) or Text = "" Then Exit Function
Set Ados=Server.CreateObject("Adodb.Stream")
Ados.Mode = 3
Ados.Type = 1
Ados.Open
Ados.LoadFromFile(Server.MapPath("gb-big5.table"))
Text = Server.UrlEnCode(Text)
b = ""
Max = Len(Text)
For i=1 To Max
c = Mid(Text,i,1)
If c = "%" Then
h = eval("&h"+Mid(Text,i+1,2))
If h < 128 Then
b = b & chr(h)
i = i+2
Else
If isvalidhex(mid(Text,i,3)) Then
If isvalidhex(mid(Text,i+3,3)) Then
l = eval("&h"+Mid(Text,i+4,2))
Ados.Position = (h-160)*510+(l-1)*2
b = b & bytes2BSTR(Ados.Read(2))
i = i+5
Else
b = b & " "
i = i+3
End If
End If
End If
Else
If c = "+" Then
b = b & " "
Else
b = b & c
End If
End If
Next
Set Ados = Nothing
gb2big5 = b
End Function
Function isvalidhex(str)
isvalidhex=true
str=ucase(str)
If Len(str) <> 3 Then isvalidhex = false : Exit Function
if Left(str,1) <> "%" Then isvalidhex = false : Exit Function
c = Mid(str,2,1)
If Not (((c>="0") And (c<="9")) or ((c>="A") And (c<="Z"))) Then isvalidhex = false : Exit Function
c = Mid(str,3,1)
If Not (((c>="0") And (c<="9")) or ((c>="A") And (c<="Z"))) Then isvalidhex = false : Exit Function
End Function
Function bytes2BSTR(vIn)
Dim strReturn
Dim i,ThisCharCode,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
%>
程序代码
' ============================================
' 格式化时间(显示)
' 参数:n_Flag
' 1:"yyyy-mm-dd hh:mm:ss"
' 2:"yyyy-mm-dd"
' 3:"hh:mm:ss"
' 4:"yyyy年mm月dd日"
' 5:"yyyymmdd"
' ============================================
Function Format_Time(s_Time, n_Flag)
Dim y, m, d, h, mi, s
Format_Time = ""
If IsDate(s_Time) = False Then Exit Function
y = cstr(year(s_Time))
m = cstr(month(s_Time))
If len(m) = 1 Then m = "0" & m
d = cstr(day(s_Time))
If len(d) = 1 Then d = "0" & d
h = cstr(hour(s_Time))
If len(h) = 1 Then h = "0" & h
mi = cstr(minute(s_Time))
If len(mi) = 1 Then mi = "0" & mi
s = cstr(second(s_Time))
If len(s) = 1 Then s = "0" & s
Select Case n_Flag
Case 1
' yyyy-mm-dd hh:mm:ss
Format_Time = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case 2
' yyyy-mm-dd
Format_Time = y & "-" & m & "-" & d
Case 3
' hh:mm:ss
Format_Time = h & ":" & mi & ":" & s
Case 4
' yyyy年mm月dd日
Format_Time = y & "年" & m & "月" & d & "日"
Case 5
' yyyymmdd
Format_Time = y & m & d
End Select
End Function
程序代码
<%
'取字段数据每个汉字的拼音首字母
function getpychar(char)
tmp=65536+asc(char)
if(tmp>=45217 and tmp<=45252) then
getpychar= "A"
elseif(tmp>=45253 and tmp<=45760) then
getpychar= "B"
elseif(tmp>=47761 and tmp<=46317) then
getpychar= "C"
elseif(tmp>=46318 and tmp<=46825) then
getpychar= "D"
elseif(tmp>=46826 and tmp<=47009) then
getpychar= "E"
elseif(tmp>=47010 and tmp<=47296) then
getpychar= "F"
elseif(tmp>=47297 and tmp<=47613) then
getpychar= "G"
elseif(tmp>=47614 and tmp<=48118) then
getpychar= "H"
elseif(tmp>=48119 and tmp<=49061) then
getpychar= "J"
elseif(tmp>=49062 and tmp<=49323) then
getpychar= "K"
elseif(tmp>=49324 and tmp<=49895) then
getpychar= "L"
elseif(tmp>=49896 and tmp<=50370) then
getpychar= "M"
elseif(tmp>=50371 and tmp<=50613) then
getpychar= "N"
elseif(tmp>=50614 and tmp<=50621) then
getpychar= "O"
elseif(tmp>=50622 and tmp<=50905) then
getpychar= "P"
elseif(tmp>=50906 and tmp<=51386) then
getpychar= "Q"
elseif(tmp>=51387 and tmp<=51445) then
getpychar= "R"
elseif(tmp>=51446 and tmp<=52217) then
getpychar= "S"
elseif(tmp>=52218 and tmp<=52697) then
getpychar= "T"
elseif(tmp>=52698 and tmp<=52979) then
getpychar= "W"
elseif(tmp>=52980 and tmp<=53640) then
getpychar= "X"
elseif(tmp>=53689 and tmp<=54480) then
getpychar= "Y"
elseif(tmp>=54481 and tmp<=62289) then
getpychar= "Z"
else '如果不是中文,则不处理
getpychar=char
end if
end function
function getpy(str)
for i=1 to len(str)
getpy=getpy&getpychar(mid(str,i,1))
next
end function
d="间接税毫汉字我喜欢"
Response.write getpy(d)
%>
更多的ASP常用函数收藏请到论坛查看: http://BBS.TC711.COM
【 双击滚屏 】 【 评论 】 【 收藏 】 【 打印 】 【 关闭 】
来源:
互联网
日期:2007-4-15