求:留言本中屏蔽html语法和限制长度的完整代码!-----103分

freeally 2002-01-17 11:11:31
谢谢大家!
...全文
140 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
freeally 2002-01-17
  • 打赏
  • 举报
回复
jamex(),你的代码中的str是什么?是不是文本输入框的name?如:<textarea cols="28" name="main" rows="4" wrap="PHYSICAL"></textarea>中的main?
这个行不行:rs("名字")=Server.HtmlEncode(name)
rs("留言")=Server.HtmlEncode(main)
??谢谢大家!

rouser 2002-01-17
  • 打赏
  • 举报
回复
我平时就用 jamex所说的那个函数
:-)
Studio 2002-01-17
  • 打赏
  • 举报
回复
大哥们 为什么不用Server.HTMLEncode呢?
难道他处理HTML不好吗?
jamex 2002-01-17
  • 打赏
  • 举报
回复
不用那么复杂,给你一个通用函数

'html字符串
function HtmlStr(str)
dim dist
dist=replace(str,"&","&")
'dist=replace(str,"'","''")
dist=replace(dist,"<","<")
dist=replace(dist,">",">")
dist=replace(dist,"chr(60)","<")
dist=replace(dist,"chr(37)",">")
dist=replace(dist,"""",""")
'dist=replace(dist,";",";;")
dist=replace(dist,chr(13),"<br>" & chr(13)+chr(10))
dist=replace(dist,chr(32)," ")
HtmlStr=dist
end function
jadesun 2002-01-17
  • 打赏
  • 举报
回复
贴段较详细的,长度不在内,用个LEN就行了

<%
FUNCTION txtToURL(tekst)
Tekst_temp = tekst


Tekst_temp = Replace(Tekst_temp,"<","<")
Tekst_temp = Replace(Tekst_temp,">",">")
Tekst_temp = Replace(Tekst_temp,"chr(13)","<BR>")
Tekst_temp = Replace(Tekst_temp,"#","<BR>")

Tekst_temp = Replace(Tekst_temp,Chr(13),"<BR>")
Tekst_temp = Replace(Tekst_temp,"[","<B>")
Tekst_temp = Replace(Tekst_temp,"]","</B>")

i = 1

While not InStr(i,Tekst_temp,"{") = 0
start = CInt( InStr(1,Tekst_temp,"{") )
i = start
slutt = CInt( InStr(start,Tekst_temp,"}") )
hole = mid(Tekst_temp,(start),(slutt-start+1))
if InStr(hole,"$") then
skille = CInt( InStr(start,Tekst_temp,"$") )
navn = mid(Tekst_temp,(start+1),(skille-start-1))
link = mid(Tekst_temp,(skille+1),(slutt-skille-1))
if Left(link,4) = "www." then
link = "http://" & link
end if 'www
if not InStr(link,"@") = 0 then
link = "mailto:" & link
end if '@
out_var = "<A HREF=""" & link & """>" & navn & "</A>"
Tekst_temp = Replace(Tekst_temp,hole,out_var,1,1)
else
link = mid(Tekst_temp,(start+1),(slutt-start-1))
if Left(link,4) = "www." then
link = "http://" & link
end if 'www
if not InStr(link,"@") = 0 then
link = "mailto:" & link
end if '@
link = "<A HREF=""" & link & """>"
Tekst_temp = Replace(Tekst_temp,"{",link,1,1)
Tekst_temp = Replace(Tekst_temp,"}","</A>",1,1)
end if
wend

txtToURL = Tekst_temp
END FUNCTION
%>
cpplus 2002-01-17
  • 打赏
  • 举报
回复
长度比较简单判断 document.YourFormName.YourTextAreaName.length > 你规定的长度
就好了
下面是替换TAG的

<%
function htmlencode2(str)
dim result
dim l
if isNULL(str) then
htmlencode2=""
exit function
end if
l=len(str)
result=""
dim i
for i = 1 to l
select case mid(str,i,1)
'case "<"
' result=result+"<"
'case ">"
' result=result+">"
case chr(13)
result=result+"<br>"
case chr(34)
result=result+"""
case "&"
result=result+"&"
case chr(32)
'result=result+" "
if i+1<=l and i-1>0 then
if mid(str,i+1,1)=chr(32) or mid(str,i+1,1)=chr(9) or mid(str,i-1,1)=chr(32) or mid(str,i-1,1)=chr(9) then
result=result+" "
else
result=result+" "
end if
else
result=result+" "
end if
case chr(9)
result=result+" "
case else
result=result+mid(str,i,1)
end select
next
htmlencode2=result
end function
%>

Studio 2002-01-17
  • 打赏
  • 举报
回复
Server.HTMLEncode("<img>") ‘对HTML编码
长度简单啊
客户端:<input ... maxlength="100">
服务端:IF length("...")>100 THEN ’剩下自己处理
freeally 2002-01-17
  • 打赏
  • 举报
回复
谢谢各位朋友,不过你们的代码我还是看不懂呀!能不能搞点注释?
haiznan 2002-01-17
  • 打赏
  • 举报
回复
我给你代码!
你要给分哦
!·
'#######################################################################

'转换html代码的函数

'########################################################################

function htmlencode(str)
on error resume next
dim result
dim l
if isnull(str) then
htmlencode=""
exit function
end if

l=len(str)
result=""
dim i
for i = 1 to l
select case mid(str,i,1)
case "<"
result=result+"<"
case ">"
result=result+">"
case chr(34)
result=result+"""
case "&"
result=result+"&"
case chr(13)
result=result+"<br>"
case chr(9)
result=result+"    "
case chr(32)
'result=result+" "
if i+1<=l and i-1>0 then
if mid(str,i+1,1)=chr(32) or mid(str,i+1,1)=chr(9) or mid(str,i-1,1)=chr(32) or mid(str,i-1,1)=chr(9) then
result=result+" "
else
result=result+" "
end if
else
result=result+" "
end if
case else
result=result+mid(str,i,1)
end select
next
if err.number<>0 then err.clear
htmlencode=result
end function

#############################
限制长度
########################
document.YourFormName.YourTextAreaName.length > 你规定的长度

langziacai 2002-01-17
  • 打赏
  • 举报
回复
可以用<%=replace(rs("**"),chr(13)&chr(10))%>,我认为是这样的不知道大虾们的看法如何?
:-),因为我是新手上路!!!
tonnycncn 2002-01-17
  • 打赏
  • 举报
回复
试试我的函数
Function LeftTrue(str,n)
If len(str)<=(n+2)/2 Then
LeftTrue=str
Else
Dim TStr
Dim l,t,c
Dim i
l=len(str)
t=l
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
If t>n Then exit for
TStr=TStr&(mid(str,i,1))
next
LeftTrue = TStr&"..."
End If
End Function
KnowLittle 2002-01-17
  • 打赏
  • 举报
回复
这怎么行呢?你要获得输入的值,必须用request对象
rs("名字")=Server.HtmlEncode(request.form("name"))

rs("留言")=Server.HtmlEncode(request.form("main"))

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧