[收集]ASP应用中的应用函数1【转自www.bitsCN.com】
1,登录验证函数
<%
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
%>
参数说明:
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")
%>
调试地址:http://www.cnbruce.com/test/function/regist.asp
2,经常有可能对某个事物进行当前状态的判断,一般即做一字段(数值类型,默认滴?)
通过对该字段值的修改达到状态切换的效果。那么,我又做了个函数,让自己轻松轻松。
<%
Function pvouch(tablename,fildname,autoidname,indexid)
dim fildvalue
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from "&tablename&" where "&autoidname&"="&indexid
rs.Open sql,conn,2,3
fildvalue=rs(""&fildname&"")
if fildvalue=0 then
fildvalue=1
else
fildvalue=0
end if
rs(""&fildname&"")=fildvalue
rs.update
rs.close
Set rs = Nothing
End Function
%>
参数说明:
pvouch(tablename,fildname,autoidname,indexid)
tablename 该事物所在数据库中的表名
fildname 该事物用以表明状态的字段名(字段类型是数值型)
autoidname 在该表中的自动编号名
indexid 用以修改状态的对应自动编号的值
引用示例如下:
<%
dowhat=request.QueryString("dowhat")
p_id=cint(request.QueryString("p_id"))
if dowhat="tj" and p_id<>"" then
call pvouch("cn_products","p_vouch","p_id",p_id)
end if
%>
<%if rs("p_vouch")=0 then%>
<a href=showpro.asp?dowhat=tj&p_id=<%=rs("p_id")%>>推荐</a>
<%else%>
<a href=showpro.asp?dowhat=tj&p_id=<%=rs("p_id")%>>取消推荐</a>
<%end if%>
3.HTML转换函数
动作转换成HTML
Function HTMLEncode(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(34), """)
Str = Replace(Str, CHR(39), "’")
Str = Replace(Str, CHR(13), "")
Str = Replace(Str, CHR(10), "<br>")
HTMLEncode = Str
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
4.日期转换函数
Function DateToStr(DateTime,ShowType) ’日期转换函数
Dim DateMonth,DateDay,DateHour,DateMinute
DateMonth=Month(DateTime)
DateDay=Day(DateTime)
DateHour=Hour(DateTime)
DateMinute=Minute(DateTime)
If Len(DateMonth)<2 Then DateMonth="0"&DateMonth
If Len(DateDay)<2 Then DateDay="0"&DateDay
If Len(DateMinute)<2 Then DateMinute="0"&DateMinute
Select Case ShowType
Case "Y-m-d"
DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay
Case "Y-m-d H:I A"
Dim DateAMPM
If DateHour>12 Then
DateHour=DateHour-12
DateAMPM="PM"
Else
DateHour=DateHour
DateAMPM="AM"
End If
If Len(DateHour)<2 Then DateHour="0"&DateHour
DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&" "&DateHour&":"&DateMinute&" "&DateAMPM
Case "Y-m-d H:I:S"
Dim DateSecond
DateSecond=Second(DateTime)
If Len(DateHour)<2 Then DateHour="0"&DateHour
If Len(DateSecond)<2 Then DateSecond="0"&DateSecond
DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&" "&DateHour&":"&DateMinute&":"&DateSecond
Case "YmdHIS"
DateSecond=Second(DateTime)
If Len(DateHour)<2 Then DateHour="0"&DateHour
If Len(DateSecond)<2 Then DateSecond="0"&DateSecond
DateToStr=Year(DateTime)&DateMonth&DateDay&DateHour&DateMinute&DateSecond
Case "ym"
DateToStr=Right(Year(DateTime),2)&DateMonth
Case "d"
DateToStr=DateDay
Case Else
If Len(DateHour)<2 Then DateHour="0"&DateHour
DateToStr=Year(DateTime)&"-"&DateMonth&"-"&DateDay&" "&DateHour&":"&DateMinute 网管网bitsCN.com
End Select
End Function
5.删除附件函数:
sub Delfile(filepath)
on error resume next
set DelObj=Server.CreateObject("Scripting.FileSystemObject")
filepath="../"&filepath
Delpath=server.mappath(filepath)
’ response.write delpath&"<br>"
set DelFi=DelObj.getfile(Delpath)
DelFi.Delete
set Delobj=nothing
end sub
6.提交表单时出现的提示框:
<html>
<head>
<SCRIPT language=javascript>
function ConfirmDel()
{
if(confirm("确定要删除选中的产品吗?一旦删除将不能恢复!"))
return true;
else
return false;
}
</SCRIPT>
</head>
<body>